Pandas.plot 多个绘图相同的图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47541127/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:49:53  来源:igfitidea点击:

Pandas.plot Multiple plot same figure

pythonpandasmatplotlibplot

提问by Boat

I have multiple CSV files that I am trying to plot in same the figure to have a comparison between them. I already read some information about pandas problem not keeping memory plot and creating the new one every time. People were talking about using an ax var, but I do not understand it...

我有多个 CSV 文件,我试图在同一个图中绘制它们以进行比较。我已经阅读了一些关于Pandas问题的信息,而不是每次都保留内存图并创建新的。人们在谈论使用 ax var,但我不明白......

For now I have:

现在我有:

def scatter_plot(csvfile,param,exp):
    for i in range (1,10):
        df = pd.read_csv('{}{}.csv'.format(csvfile,i))
        ax = df.plot(kind='scatter',x=param,y ='Adjusted')
        df.plot.line(x=param,y='Adjusted',ax=ax,style='b')
    plt.show()
    plt.savefig('plot/{}/{}'.format(exp,param),dpi=100)

But it's showing me ten plot and only save the last one. Any idea?

但它向我展示了十个情节并且只保存了最后一个。任何的想法?

Thanks

谢谢

回答by ImportanceOfBeingErnest

The structure is

结构是

  1. create an axes to plot to
  2. run the loop to populate the axes
  3. save and/or show (save before show)
  1. 创建要绘制的轴
  2. 运行循环以填充轴
  3. 保存和/或显示(显示前保存)

In terms of code:

在代码方面:

import matplotlib.pyplot as plt
import pandas as pd

ax = plt.gca()
for i in range (1,10):
    df = pd.read_csv(...)
    df.plot(..., ax=ax)
    df.plot.line(..., ax=ax)

plt.savefig(...)
plt.show()