Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved

Solution 1:

As noted by @andrew, the ipython magics are enforcing bbox_inches='tight' by default. This can be overridden using other magics as explained in the ipython documentation:

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

produces an inline image identical to that produced by savefig.

Solution 2:

The behavior is due to the fact that the magic %matplotlib inline defaults to using the bbox_inches='tight' when rendering inline.

I know you asked about changing the behavior of plt.show(), but alternatively, you could change the behavior of savefig() to use the same settings as the notbeook.

fig.savefig('mypath.png',dpi=300, facecolor='gray',  bbox_inches='tight')

New 'savefig' image:

enter image description here