Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save pandas plot with subplots to one file

I am creating subplots in pandas/jupyter notebook from the following dataframe

METHOD1       A           B             C              D             E
METHOD2                                                                
high       1410          14           426          13781             1
low       74142         303        757024          95105            37
medium    99174         670        277013         640000           127
mono      46599         207        405108          16793           160

axs = ct.plot(kind='barh', subplots=True, legend=False, figsize=(24,16))
for ax in axs:
    ax.set_xscale('log')

In Jupyter I get one image with 4 subplots. I want to save that plot to one png, but

fig=axs.get_figure()
fig.savefig('plot.png')

gives an error message

AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'

because axs is an array of the subplots I can save the individual subplots.

How can I save all the subplots to one image?

like image 620
Fenrir Avatar asked Dec 13 '17 13:12

Fenrir


People also ask

How do I save a plot in pandas?

When you use Pandas to plot graphs, the pd. plot() function returns the Matplotlib axis object which can be used to make changes to the graph and to save it in later cells in the Jupyter notebook.

How do I save all figures in Python?

MatPlotLib with Python Create a new figure (fig2) or activate an existing figure using figure() method. Plot the Second line using plot() method. Initialize a variable, filename, to make a pdf file. Create a user-defind function, save_multi_image, and call it to save all the open matplotlib figures in one file at once.

How do I save a subplot in Python?

Matplotlib plots can be saved as image files using the plt. savefig() function.


1 Answers

Do axs[0].get_figure().

You are trying to call a matplotlib method on a numpy array.

like image 193
IMCoins Avatar answered Oct 02 '22 09:10

IMCoins