gpt4 book ai didi

python - Matplot : How to plot true/false or active/deactive data?

转载 作者:太空狗 更新时间:2023-10-29 17:57:57 26 4
gpt4 key购买 nike

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

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

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

最佳答案

你要找的是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 标签。

enter image description here

看来你问题中的图像在图像中有一些插值。让我们再设置一些东西:

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)

创造 enter image description here

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

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)

创造

enter image description here

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

关于python - Matplot : How to plot true/false or active/deactive data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com