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

后端 未结 5 2067
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:51

    You can use ax.figure.savefig(), as suggested in a comment on the question:

    import pandas as pd
    
    df = pd.DataFrame([0, 1])
    ax = df.plot.line()
    ax.figure.savefig('demo-file.pdf')
    

    This has no practical benefit over ax.get_figure().savefig() as suggested in other answers, so you can pick the option you find the most aesthetically pleasing. In fact, get_figure() simply returns self.figure:

    # Source from snippet linked above
    def get_figure(self):
        """Return the `.Figure` instance the artist belongs to."""
        return self.figure
    

提交回复
热议问题