Why does Jupyter Notebook need figure and axes objects in the same cell to show a figure

I am trying to create a simple plot using matplotlib to draw a figure in Jupyter Lab. Below is a snapshot of the code.

enter image description here

I create two figure objects (fig_1 and fig_2) and then add axes to them. The axes show up in the first case when I have both the fig_1 and ax_1 objects in the same cell.

But in the second case, the image is not rendered, if the objects (fig_2 and ax_2 are in different cells).

The scope of these objects should extend beyond the cells.

Why does the image not appear in the second case?

Forgive me, if this sounds too trivial. But I am new to Jupyter Notebooks, Python and matplotlib. I am using Anaconda 4.5 with Python 3.6.5. I did try to look around but could not find an answer.

1 answer

  • answered 2018-11-08 08:26 Diziet Asahi

    I don't know enough about the notebook's internal to answer the question why, but I can tell you that if you want to display the figure in another cell, after its creation, just just have to call its instance in the cell:

    enter image description here