Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter notebook matplotlib figures show up small until cell is completed

I'm trying to make a notebook where the data produced by a long for loop is put in a graph point by point. However, when using %matplotlib notebook and fig.canvas.draw() the graph is tiny up until the cell finishes running. (In fact, I've got many of those graphs and they are even tinyer when using more subplots.)

Here my code reproducing the behaviour in a jupyter notebook, at least on OS X with (latest) jupyter-core 4.3.0 and (latest) matplotlib 2.0.2.

%matplotlib notebook
import time
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)
for _ in range(5):
    ax.plot([1,2,3], [1,2,3])
    fig.canvas.draw()
    time.sleep(1)

During the cell execution I get this plot

Small image

And when the cell finishes execution (after 5 seconds) I get this

enter image description here

I would like to get the larger image even during cell execution. What am I doing wrong?

like image 937
mitch Avatar asked Jul 29 '17 00:07

mitch


People also ask

How do you increase the size of a figure in jupyter notebook?

Next, to increase the size of the plot in the jupyter notebook use plt. rcParams[“figure. figsize”] method and set width and height of the plot.

What does PLT mean in Matplotlib?

Basically, the plt is a common alias of matplotlib. pyplot used by most people. When we plot something using plt such as plt. line(...) , we implicitly created a Figure instance and an Axes inside the Figure object.

Is PLT show necessary in Jupyter?

Plotting from an IPython shell draw() . Using plt. show() in Matplotlib mode is not required.


1 Answers

As proposed by ImportanceOfBeingErnest, one solution is to put the figure creation in its own cell.

[1] %matplotlib notebook
    import time
    import matplotlib.pyplot as plt

[2] fig, ax = plt.subplots(1, 1)

[3] for _ in range(5):
        ax.plot([1,2,3], [1,2,3])
        fig.canvas.draw()
        time.sleep(1)

Edit: This solutions does not work if you run all cells at once.

like image 184
mitch Avatar answered Oct 15 '22 08:10

mitch