Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig

后端 未结 5 2085
Happy的楠姐
Happy的楠姐 2020-12-02 07:18

I\'m using pandas to generate a plot from a dataframe, which I would like to save to a file:

dtf = pd.DataFrame.from_records(d,columns=h)
fig = plt.figure()
         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 07:46

    So I'm not entirely sure why this works, but it saves an image with my plot:

    dtf = pd.DataFrame.from_records(d,columns=h)
    dtf2.plot()
    fig = plt.gcf()
    fig.savefig('output.png')
    

    I'm guessing that the last snippet from my original post saved blank because the figure was never getting the axes generated by pandas. With the above code, the figure object is returned from some magic global state by the gcf() call (get current figure), which automagically bakes in axes plotted in the line above.

提交回复
热议问题