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).
To save plot figure as JPG or PNG file, call savefig() function on matplotlib. pyplot object. Pass the file name along with extension, as string argument, to savefig() function.
Matplotlib plots can be saved as image files using the plt. savefig() function. The plt. savefig() function needs to be called right above the plt.
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
AttributeError: 'numpy.ndarray' object has no attribute 'get_figure'
, then it is likely that you are plotting multiple columns. 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With