Notebook: %matplotlib inline and savefig differ

Created on 7 Jul 2017  路  2Comments  路  Source: jupyter/notebook

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()
~~~

Most helpful comment

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}

All 2 comments

Matplotlib issue

plt.savefig(..., bbox_inches='tight')

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}
Was this page helpful?
0 / 5 - 0 ratings