说我有一个数字A由10行组成:1到10,matplotlib的默认设置着色。可能在matplotlib中的不同地块上保留相同颜色的相同线条?
现在我想绘制图B中的第1-5行和图C中的第6-10行,但保持线条的颜色一致。我的意思是在图B中,1-5线的颜色与图A中的1-5线相同;在图C中,线6-10的颜色与图A中的线6-10相同。
有没有办法做到这一点?先谢谢你!
说我有一个数字A由10行组成:1到10,matplotlib的默认设置着色。可能在matplotlib中的不同地块上保留相同颜色的相同线条?
现在我想绘制图B中的第1-5行和图C中的第6-10行,但保持线条的颜色一致。我的意思是在图B中,1-5线的颜色与图A中的1-5线相同;在图C中,线6-10的颜色与图A中的线6-10相同。
有没有办法做到这一点?先谢谢你!
对于第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()
如果通过分配标签来连接您的图(如果您有图例则很有用),那么您可以查找以前的颜色。
原来的问题:
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")
谢谢你的工作! –