pandas 在所有子图中绘制带有列的 DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35655671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting DataFrame with column in all subplots
提问by TomCho
Consider that I have a pandas DataFrame with 3 columns. I would to plot two of these columns as separate subplots and the other one should appear on both the other subplots.
考虑到我有一个包含 3 列的 Pandas DataFrame。我会将这些列中的两列绘制为单独的子图,而另一列应该出现在其他两个子图中。
To explain better, consider the example
为了更好地解释,考虑这个例子
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])
Which is close to what I want. But not exactly. I would like that plot to have only two subplots. One with columns 0 and 1, and the other with columns 0 and 2. As is comparing each column to another column. The closest I got was
这接近我想要的。但不完全是。我希望那个情节只有两个子情节。一个包含第 0 列和第 1 列,另一个包含第 0 列和第 2 列。将每一列与另一列进行比较。我得到的最接近的是
df[[1,2]].plot(subplots=True, layout=(1,2), ylim=[0,40])
plt.plot(df.index, df[0], label=0)
which gives me this picture, where column 0 only appears in the second subplot, and not on all of them:
这给了我这张图片,其中第 0 列仅出现在第二个子图中,而不是所有子图中:
Is there any way to do this? Preferably not going completely outside of pandas. I know I could just iterate through columns 1 and 2 and manually plot them with pyplot alongside column 0, but that's just too much code and I'd like to avoid that, if possible.
有没有办法做到这一点?最好不要完全离开Pandas。我知道我可以遍历第 1 列和第 2 列,并在第 0 列旁边使用 pyplot 手动绘制它们,但这只是代码太多,如果可能,我想避免这种情况。