【问题标题】:Matplot: How to plot true/false or active/deactive data?Matplot:如何绘制真/假或活动/非活动数据?
【发布时间】:2014-10-17 15:52:33
【问题描述】:

我想绘制类似于下图的true/falseactive/deactive 二进制数据:

横轴是时间,纵轴是一些实体(这里是一些传感器),它们是活动的(白色)或非活动的(黑色)。如何使用pyplot 绘制这样的图表。

我搜索了这些图表的名称,但找不到。

【问题讨论】:

    标签: python matplotlib plot scipy


    【解决方案1】:

    你要找的是imshow:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # get some data with true @ probability 80 %
    data = np.random.random((20, 500)) > .2
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.imshow(data, aspect='auto', cmap=plt.cm.gray, interpolation='nearest')
    

    那么你只需要从某个地方获取 Y 标签。

    您问题中的图像似乎在图像中有一些插值。让我们再设置一些东西:

    import matplotlib.pyplot as plt
    import numpy as np
    
    # create a bit more realistic-looking data
    # - looks complicated, but just has a constant switch-off and switch-on probabilities
    #   per column
    # - the result is a 20 x 500 array of booleans
    p_switchon = 0.02
    p_switchoff = 0.05
    data = np.empty((20,500), dtype='bool')
    data[:,0] = np.random.random(20) < .2
    for c in range(1, 500):
        r = np.random.random(20)
        data[data[:,c-1],c] = (r > p_switchoff)[data[:,c-1]]
        data[-data[:,c-1],c] = (r < p_switchon)[-data[:,c-1]]
    
    # create some labels
    labels = [ "label_{0:d}".format(i) for i in range(20) ]
    
    # this is the real plotting part
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.imshow(data, aspect='auto', cmap=plt.cm.gray)
    ax.set_yticks(np.arange(len(labels)))
    ax.set_yticklabels(labels)
    

    创造

    但是,插值在这里不一定是好事。为了使不同的行更容易分开,可以使用颜色:

    import matplotlib.pyplot as plt
    import matplotlib.colors
    import numpy as np
    
    # create a bit more realistic-looking data
    # - looks complicated, but just has a constant switch-off and switch-on probabilities
    #   per column
    # - the result is a 20 x 500 array of booleans
    p_switchon = 0.02
    p_switchoff = 0.05
    data = np.empty((20,500), dtype='bool')
    data[:,0] = np.random.random(20) < .2
    for c in range(1, 500):
        r = np.random.random(20)
        data[data[:,c-1],c] = (r > p_switchoff)[data[:,c-1]]
        data[-data[:,c-1],c] = (r < p_switchon)[-data[:,c-1]]
    
    # create some labels
    labels = [ "label_{0:d}".format(i) for i in range(20) ]
    
    # create a color map with random colors
    colmap = matplotlib.colors.ListedColormap(np.random.random((21,3)))
    colmap.colors[0] = [0,0,0]
    
    # create some colorful data:
    data_color = (1 + np.arange(data.shape[0]))[:, None] * data
    
    # this is the real plotting part
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.imshow(data_color, aspect='auto', cmap=colmap, interpolation='nearest')
    ax.set_yticks(np.arange(len(labels)))
    ax.set_yticklabels(labels)
    

    创造

    当然,您会希望使用不那么奇怪的配色方案,但这完全取决于您的艺术观点。这里的诀窍是n 行上的所有True 元素都具有n+1 的值,并且所有False 元素在data_color 中都是0。这使得创建颜色映射成为可能。当然,如果您想要一个具有两种或三种颜色的循环颜色图,只需使用imshow 中的data_color 的模数,例如data_color % 3.

    【讨论】:

    • 感谢您的回答!你知道这些图表的名称吗?
    • +1 似乎原件也是使用 Matplotlib 制作的,看起来完全一样。对于它的价值,我发现插值的使用对该应用程序具有误导性,这表明冰箱和电话之间存在逐渐过渡!我更喜欢直条,但也许可以选择一些漂亮的颜色。
    • @Constantine:我会称之为时间线图,但我真的不知道是否有更好的名字。不幸的是,功能区图是为其他东西保留的。
    • @BasSwinckels:是的,插值在这里并不是最好的。我添加了一个带颜色的非插值版本(不知道颜色的好坏)。从可视化的角度来看,我实际上会在不同的块之间有三行数据(彩色/深色)和一个空行(黑色),因为这样可以更容易地跟踪每一行。
    • 非常高质量的答案。这值得某种奖励。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多