Matplotlib and Ipython-Notebook: Displaying Exactly the Figure That Will Be Saved

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

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.

Get Jupyter notebook to display matplotlib figures in real-time

I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,

%matplotlib inline

import matplotlib.pyplot as plt

def do_a_1_second_calculation():
plt.pause(1)

def show_figure(i):
plt.figure(i)
plt.plot([1,i,3])
plt.show()

for i in range(10):
print(i)
show_figure(i)
do_a_1_second_calculation()

results in the desired outcome

Sample Image

Matplotlib: Save figure as file from iPython notebook

Problem solved: add 'bbox_inches='tight' argument to savefig.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.plot([1,2])

savefig('test.png', bbox_inches='tight')

I don't understand what's happening here, but the file looks like the iPython notebook inline file now. Yay.

prevent plot from showing in jupyter notebook

Perhaps just clear the axis, for example:

fig= plt.figure()
plt.plot(range(10))
fig.savefig("save_file_name.pdf")
plt.close()

will not plot the output in inline mode. I can't work out if is really clearing the data though.

Saving plot from ipython notebook produces a cut image

By default, matplotlib leaves very little room for x and y axis labels and tick labels, therefore you need to adjust the figure to include more padding. Fortunately this could not be easier to do. Before you call savefig, you can call call

fig.tight_layout()
plt.savefig('output.png', dpi=300)

Alternatively, you can pass bbox_inches='tight' to savefig which will also adjust the figure to include all of the x and y labels

plt.savefig('output.png', dpi=300, bbox_inches='tight')

How do I show the same matplotlib figure several times in a single IPython notebook?

Just call myFigure in any cell after myFigure was assigned in cell before.

For example in cell 1:

In [1]:
myFigure, myAx = plt.subplots()
myAx.plot([1,2,3])

In cell after that:

In [2]:
myFigure

This will show myFigure

How to make IPython notebook matplotlib plot inline

I used %matplotlib inline in the first cell of the notebook and it works. I think you should try:

%matplotlib inline

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

You can also always start all your IPython kernels in inline mode by default by setting the following config options in your config files:

c.IPKernelApp.matplotlib=<CaselessStrEnum>
Default: None
Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
Configure matplotlib for interactive use with the default matplotlib backend.


Related Topics



Leave a reply



Submit