2016-02-26 29 views
2

请考虑我有一个带有3列的pandas DataFrame。我会将这些列中的两个绘制为单独的子图,另一个应该出现在其他子图上。用所有子图中的列绘制DataFrame

更好地说明,考虑例如

x = np.linspace(0,10,5) 
y1 = 2*x 
y2 = 3*x 
y3 = 4*x 

df = pd.DataFrame(index=x, data=zip(*[y1,y2,y3])) 
df.plot(subplots=True, layout=(1,3), ylim=[0,40]) 

这个代码显示 enter image description here

这是接近我想要的。但不完全。我希望那个情节只有两个子情节。一列有0和1列,另一列有0和2列。正如比较每列到另一列。我得到的最接近是

df[[1,2]].plot(subplots=True, layout=(1,2), ylim=[0,40]) 
plt.plot(df.index, df[0], label=0) 

这给了我这张照片,其中列0只出现在第二个插曲,而不是所有的人:

enter image description here

有没有办法做到这个?最好不要完全在大熊猫之外。我知道我可以遍历第1列和第2列,并手动将它们与第0列中的pyplot一起绘制,但这只是太多代码,如果可能,我想避免这种情况。

回答

2

像这样

ax = df[[1,2]].plot(subplots=True, layout=(1,2), ylim=[0,40]) 

for axe in ax[0]: 
    axe.plot(df.index, df[0]) 

enter image description here

+0

诀窍(指出,如果不是很明显)是大熊猫'plot'返回matplotlib轴。 – IanS

+0

在最后一行添加'label = 0'会产生什么影响? –

+0

这是无用的,好评:-)它在这里,因为它是在问题中,我保持原样。我已更新我的答案以将其删除。谢谢你的评论。 – Romain