Matplot:如何绘制正确/错误或活动/非活动数据? [英] Matplot: How to plot true/false or active/deactive data?

查看:39
本文介绍了Matplot:如何绘制正确/错误或活动/非活动数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个 true/false active/deactive 二进制数据,类似于下图:

I want to plot a true/false or active/deactive binary data similar to the following picture:

横轴是时间,纵轴是一些实体(这里是一些传感器),它们是活动的(白色)或不活动的(黑色).如何使用 pyplot 绘制此类图形.

The horizontal axis is time and the vertical axis is some entities(Here some sensors) which is active(white) or deactive(black). How can I plot such a graphs using pyplot.

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

I searched to find the name of these graphs but I couldn't find it.

推荐答案

您正在寻找的是 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标签即可.

Then you will just have to get the Y labels from somewhere.

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

It seems that the image in your question has some interpolation in the image. Let us set a few more things:

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)

创建

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

However, the interpolation is not necessarily a good thing here. To make the different rows easier to separate, one might use colors:

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 .

Of course, you will want to use something less strange as the coloring scheme, but that is really up to your artistic views. Here the trick is that all True elements on row n have value n+1 and, and all False elements are 0 in data_color. This makes it possible to create a color map. Naturally, if you want a cyclic color map with two or three colors, just use the modulus of data_color in imshow by, e.g. data_color % 3.

这篇关于Matplot:如何绘制正确/错误或活动/非活动数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆