这个图叫什么以及如何在 matplotlib 中制作它?
What is this plot called and how to make it in matplotlib?
假设我有这样的图表
plot(np.random.rand(10))
pylab.ylim([-0,1.5])
我有这样的状态序列
In[141]:np.random.randint(5,size=(1, 10))
Out[142]:array([[2, 2, 4, 2, 1, 2, 0, 0, 4, 4]])
我想像这样在上面的图上叠加那个状态序列:
但是如何在 matplotlib 中做到这一点?
总结一下:我希望你的情节的不同部分有不同颜色的背景,这取决于状态,每个独特的状态都有独特的颜色。
要将绘图分成不同颜色的部分,您可以使用 axvspan
函数。
这要求您知道各州的 x 坐标边界。
假设状态总是 L
个单位长,并且 STATE_COLOR
是状态编号和 matplotlib 颜色之间的映射('r'、'k'、'b' ...) 然后你得到以下内容:
# States and their colors
state_list = [1, 2, 1]
STATE_COLOR = { 1 : 'r', 2 : 'y' }
L = 1.5 # Constant state length
x = np.linspace(0, 4.5)
y = x**2 - 3
# Draw states
for i, state in enumerate(state_list):
x1 = i * L
x2 = (i+1) * L
plt.axvspan(x1, x2, color=STATE_COLOR[state])
# Draw line data
plt.plot(x, y)
假设我有这样的图表
plot(np.random.rand(10))
pylab.ylim([-0,1.5])
我有这样的状态序列
In[141]:np.random.randint(5,size=(1, 10))
Out[142]:array([[2, 2, 4, 2, 1, 2, 0, 0, 4, 4]])
我想像这样在上面的图上叠加那个状态序列:
但是如何在 matplotlib 中做到这一点?
总结一下:我希望你的情节的不同部分有不同颜色的背景,这取决于状态,每个独特的状态都有独特的颜色。
要将绘图分成不同颜色的部分,您可以使用 axvspan
函数。
这要求您知道各州的 x 坐标边界。
假设状态总是 L
个单位长,并且 STATE_COLOR
是状态编号和 matplotlib 颜色之间的映射('r'、'k'、'b' ...) 然后你得到以下内容:
# States and their colors
state_list = [1, 2, 1]
STATE_COLOR = { 1 : 'r', 2 : 'y' }
L = 1.5 # Constant state length
x = np.linspace(0, 4.5)
y = x**2 - 3
# Draw states
for i, state in enumerate(state_list):
x1 = i * L
x2 = (i+1) * L
plt.axvspan(x1, x2, color=STATE_COLOR[state])
# Draw line data
plt.plot(x, y)