假设我有一个由10行组成的图A:1到10,根据matplotlib的默认设置进行着色。
现在我想绘制图B中的线1-5和图C中的线6-10,但保持线的颜色一致。我的意思是在图B中,线1-5的颜色与图A中的线1-5相同;在图C中,线6-10的颜色与图A中的线6-10相同。
有办法做到这一点吗?提前谢谢你!
发布于 2016-08-26 19:07:43
如果您通过指定标签来链接绘图(如果您有图例,这一点很有用),则可以查找上一种颜色。
原始问题:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
f, axs = plt.subplots(3)
lines = [np.random.rand(10,1) for a in range(10)]
for i, line in enumerate(lines):
axs[0].plot(line)
for i, line in enumerate(lines[:5]):
axs[1].plot(line)
for i, line in enumerate(lines[5:]):
axs[2].plot(line)
axs[0].set_title("All Lines")
axs[1].set_title("First Five")
axs[2].set_title("Last Five")
f.tight_layout()
plt.savefig("No Linking.png")
然后添加一些标签:
f, axs = plt.subplots(3)
for i, line in enumerate(lines):
label = "Line {}".format(i)
axs[0].plot(line, label=label)
for i, line in enumerate(lines):
if i < 5:
ax = axs[1]
else:
ax = axs[2]
label = "Line {}".format(i)
# here we look up what colour was used in the first subplot.
colour = [l for l in axs[0].lines if l._label == label][0]._color
ax.plot(line, label=label, color=colour)
axs[0].set_title("All Lines")
axs[1].set_title("First Five")
axs[2].set_title("Last Five")
f.tight_layout()
plt.savefig("With Linking.png")
发布于 2016-06-09 05:38:15
对于第6-10行,在绘制您想要绘制的实际行之前,只需绘制5行空白行。1-5将具有与1-10方案匹配的默认颜色。
import matplotlib.pyplot as plt
plt.plot([0]) # line 1
plt.plot([0]) # line 2
plt.plot([0]) # line 3
plt.plot([0]) # line 4
plt.plot([0]) # line 5
plt.plot([1,2,3,4]) # line 6
plt.ylabel('some numbers')
plt.show()
https://stackoverflow.com/questions/37712243
复制相似问题