jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.71k stars 4.95k forks source link

%matplotlib inline and savefig differ #2640

Open rgaiacs opened 7 years ago

rgaiacs commented 7 years ago

Sorry if this isn't the correct place to report this. I didn't know for sure where to report. Let me know the correct place and I will move it.

Summary

(matplotlib) Inline plot is fine but the same plot created with savefig has part of the visualisation cropped.

Inline example

screenshot_2017-07-07_08-54-35

savefig example

have-you-received-training-br

Code

The plot_pie() function in the first image is

def plot_pie(dataframe, **kargs):
    plot = dataframe.plot(
        kind='pie',
        autopct=make_autopct(dataframe)
    )
    plot.set_ylabel('')
    plot.set_title(kargs["title"] if "title" in kargs else dataframe.name)
    plot.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plot.figure.autolayout = True
    plot.figure.savefig(kargs["filename"] if "filename" in kargs else "plot.png")

Environment

$ python --version 
Python 3.5.3 :: Continuum Analytics, Inc.
$ jupyter --version       
4.3.0
$ jupyter notebook --version
5.0.0.dev
$ python
Python 3.5.3 |Continuum Analytics, Inc.| (default, Mar  6 2017, 11:58:13) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> pandas.__version__
'0.20.2'
>>> import matplotlib
>>> matplotlib.__version__
'2.0.2'
>>> exit()
ahartikainen commented 7 years ago

Matplotlib issue

plt.savefig(..., bbox_inches='tight')
ImportanceOfBeingErnest commented 4 years ago

It's more an ipython/ipykernel issue, because the inline backend hardcodes bbox_inches='tight' as seen in https://github.com/ipython/ipykernel/blob/7b2727d827a95d2059c9d9d9354899feabb642ee/ipykernel/pylab/config.py#L83

A solution is to change the config, e.g. via

%matplotlib inline
%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}