3

I'm learning Matplotlib and using a Jupyter notebook to track each thing that I learn. However, I ran into a problem because I have multiple cells with matplotlib code. In one of my first cells, I run plt.show(), which outputs a plot beneath the cell. Further down the page, I have some other code which plots new points, resizes an axis, etc., then runs plt.show()....which works, but applies the changes to the original plot that was created after the first cell.

Is there any way to get a new plot window to display beneath whichever cell I am running?

(The reason I want to do this: The first cell might be an example showing how to plot a basic set of points. I want this to display its own simple plot. Further down the page, I resize axes and change the style of graph. However, when this plots, I want to see a separate plot, or maybe the same plot redone (as in, it can keep the original points I plotted -- no need to clear the whole thing) but with the new changes, beneath this more complex cell.)

UPDATE: Images.

In Image 1, I have run the first cell of code. The graph displays beneath the cell. Just as I want. enter image description here

In this second image, I've now run the lower block of code (marked [3]). The changes, however, are applied to the plot sitting above it, because that's where it was originally created. But I'd like a new plot, or maybe not a clean new plot, but at least some way to make that plot display beneath cell [3] that I just ran. enter image description here

Alex G
  • 747
  • 4
  • 15
  • 27
  • That worked! Interesting. I originally made it `%matplotlib notebook` because that allows interactivity. However, it appears interactivity gives this behavior, because the original plot can still update. Therefore, rather than make a new plot beneath each code block, it simply updates the original plot. I was able to get it to work using both your solution, or, using `%matplotlib notebook`, by adding the line `plt.ioff()`. Wish there was a way to have interactivity with a new plot, though. Any way to force a new plot? – Alex G Dec 31 '16 at 07:10
  • You're awesome. That worked, thank you! – Alex G Dec 31 '16 at 07:15
  • Do you want to just answer it so I can mark this? – Alex G Dec 31 '16 at 07:15
  • Possibly relevant here: https://stackoverflow.com/questions/45760693/how-to-overlay-plots-from-different-cells – ImportanceOfBeingErnest May 03 '18 at 12:46

1 Answers1

3

In the comments, you mentioned that you're using the %matplotlib notebook magic, because it allows interactivity.

One option is to stop using interactivity.

As you found, you can turn off interactivity with plt.ioff(). You could also stop using %matplotlib notebook altogether and instead use %matplotlib inline (called at the top of the notebook). With %matplotlib inline, you don't need to call plt.show().

But you want to use interactivity.

So what you should do is define a new figure after you've plotted your first figure. To do this, call plt.figure() after the first plot, before the code for the second.

abcd
  • 10,215
  • 15
  • 51
  • 85