2016-06-08 38 views

回答

-2

对于第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() 
+0

谢谢你的工作! –

1

如果通过分配标签来连接您的图(如果您有图例则很有用),那么您可以查找以前的颜色。

原来的问题:

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") 

No Linking of colours

然后添加一些标签来代替:

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") 

enter image description here

相关问题