113

In ipython Notebook, first create a pandas Series object, then by calling the instance method .hist(), the browser displays the figure.

I am wondering how to save this figure to a file (I mean not by right click and save as, but the commands needed in the script).

Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
GeauxEric
  • 2,814
  • 6
  • 26
  • 33
  • This question is for a single plot of one column. See [Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig](https://stackoverflow.com/q/19555525/7758804) for dealing with subplots from plotting multiple columns. – Trenton McKinney Apr 21 '21 at 19:38

4 Answers4

237

Use the Figure.savefig() method, like so:

ax = s.hist()  # s is an instance of Series
fig = ax.get_figure()
fig.savefig('/path/to/figure.pdf')

It doesn't have to end in pdf, there are many options. Check out the documentation.

Alternatively, you can use the pyplot interface and just call the savefig as a function to save the most recently created figure:

import matplotlib.pyplot as plt
s.hist()
plt.savefig('path/to/figure.pdf')  # saves the current figure

Plots from multiple columns

  • Added from a comment toto_tico made on 2018-05-11
  • If you are getting this error AttributeError: 'numpy.ndarray' object has no attribute 'get_figure', then it is likely that you are plotting multiple columns.
    • In this case, ax will be an array of all the axes.
ax = s.hist(columns=['colA', 'colB'])

# try one of the following
fig = ax[0].get_figure()
fig = ax[0][0].get_figure()

fig.savefig('figure.pdf')
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
18

You can use ax.figure.savefig():

import pandas as pd

s = pd.Series([0, 1])
ax = s.plot.hist()
ax.figure.savefig('demo-file.pdf')

This has no practical benefit over ax.get_figure().savefig() as suggested in Philip Cloud's answer, 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
joelostblom
  • 43,590
  • 17
  • 150
  • 159
10

You can simply save your (e.g. histogram) plot like this:

df.plot.hist().get_figure().savefig('name')
m_h
  • 485
  • 5
  • 11
4

Just wanted to add that the default resolution is 100dpi, which is fine for screen but won't work if you want to enlarge or print it. You can pass a 'dpi' parameter to get a high-resolution file:

ax = s.hist()  # s is an instance of Series
ax.figure.savefig('/path/to/figure.png', dpi=300)
larapsodia
  • 594
  • 4
  • 15