在循环内绘制图形

时间:2015-10-26 20:38:40

标签: python pandas matplotlib

我在一个文件夹中有2个csv文件,如下所示:

(文件1)

Count      Bins
0       -0.322392
1       -0.319392
1       -0.316392
0       -0.313392
2       -0.310392
1       -0.307392
5       -0.304392
4       -0.301392

(文件2)

Count      Bins
5       -0.322392
1       -0.319392
1       -0.316392
6       -0.313392
2       -0.310392
1       -0.307392
2       -0.304392
4       -0.301392

我想在x轴上使用Bins和在y轴上使用Count制作折线图。所以每个图中只有一行。到目前为止我正在使用此代码:

import pandas as pd
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

#path where csv files are stored
pth = (r'F:\Sheyenne\Statistics\IDL_stats\NDII-2')

#initiate loop
for f in os.listdir(pth):
    if not os.path.isfile(os.path.join(pth,f)):
        continue
    #read each file
    df = pd.read_csv(os.path.join(pth, f))
    #add column names
    df.columns=['Count', 'Bins']
    #create pdf file to save graphs to
    with PdfPages(r'F:\Sheyenne\Statistics\IDL_stats\Delete.pdf') as pdf:
         #plot the graph
         df2=df.plot(title=str(f))
         #set x-label
         df2.set_xlabel("Bins")
         #set y-label
         df2.set_ylabel("Count")
         #save the figure
         pdf.savefig(df2)
         #close the figure
         plt.close(df2)
print "Done Processing"  

但是这会显示两行,一行用于Count,另一行用于Bins。它也只绘制第一个文件的图形而不是第二个返回错误的文件:

Traceback (most recent call last):

  File "<ipython-input-5-b86bf00675fa>", line 1, in <module>
    runfile('F:/python codes/IDL_histograms.py', wdir='F:/python codes')

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "F:/python codes/IDL_histograms.py", line 26, in <module>
    pdf.savefig(df2)

  File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2438, in savefig
    raise ValueError("No such figure: " + repr(figure))

ValueError: No such figure: <matplotlib.axes._subplots.AxesSubplot object at 0x0D628FB0>

2 个答案:

答案 0 :(得分:7)

Pandas DataFrame.plot()返回matplotlib axis object,但savefig需要一个无花果对象。使用plt.gcf()获取当前matplotlib图并保存。

# Open the pdf before looping to add pages
with PdfPages(r'C:\test\Delete.pdf') as pdf:
    for f in os.listdir(pth):
        if not os.path.isfile(os.path.join(pth,f)):
            continue
        # ignore the pdf file that just got created
        if 'pdf' in f:
            continue
        #read each file
        df = pd.read_csv(os.path.join(pth, f))
        #add column names
        df.columns=['Count', 'Bins']
        #create pdf file to save graphs to
        #plot the graph
        df2=df.plot(title=str(f))
        #set x-label
        df2.set_xlabel("Bins")
        #set y-label
        df2.set_ylabel("Count")
        #save the figure
        fig = plt.gcf()
        pdf.savefig(fig)
        #close the figure
        plt.close(fig)

适合我。

答案 1 :(得分:0)

而不是df2=df.plot(title=str(f)),而是单独绘制数据框中的所有内容,请尝试df2=df.plot(x='Bins', y='Count', title=str(f))

相关问题