Show new matplotlib graph further down Jupyter notebook - python

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.
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.

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.

Related

How do I show plot zoomed in?

I'm using matplotlib to show a plot with %matplotlib widget in jupyter lab. Almost every time I show the plot, next thing I have to do is zoom in and examine one of the sections of the plot. I would like to be able to show the plot zoomed into that section, and if I want to examine the rest of the plot, as I sometimes do, I would press back button and see all of it.
This is similar to showing subplot with relevant data or using plt.xlim, except I would like to do it in a single plot so the data I'm looking at can occupy more screen space, and plt.xlim just shows relevant stretch of data without possibility to zoom out again.
How do I do this is jupyter lab?

How to fix matplotlib overlapping graphs [duplicate]

I am trying to use a forloop to produce figures for each set of data I have, but while the .show() command produces the correct figure, .savefig() keeps adding the previous plotted values to the new figure.
In my forloop, this is the relevant sample of the code.
import matplotlib.pyplot as plt
plt.plot(X,Y[:,0],'o-')
plt.xlabel('x')
plt.savefig('plot'+str(i)+'.png')
As a comparison, here is the savefig plot and here is that shown by show(). As can be seen, the savefig() plot also plotted the previous result.
You have to close current figure after saving with function
plt.close(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.close
Or you have to clean current figure after saving by plt.clf(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clf
I made some beautiful charts before I included plt.clf() to clear the plot each time through the loop.
scatterplot1
scatterplot2
In other words, my previous plots were being added to a single figure as shown in the lots above, within my for loop as well. adding [plt.clf()] to clear the plot each time through the loop fixed this problem being clearing the figure before starting the loop iteration at the top to create a new figure with new plots.
TLDR; I included plt.clf() to clear the plot each time through the loop.

Plot Graphs in separated window

I have a long Jupyter notebook code and there is many cells, which are redrawing the actual graph plot. When I am running cells after changing their contents I need to check the plot, but I always need to scroll up and down. I would prefer to watch the plot changes in separated window (I am using two monitors), so I will change the cell content, run the cell, and then just turn my head and see the plot - without any scrolling. Is there a way how to do that? I know it can be done by Spyder, but I want to do it in Jupyter notebook, since I use a lot of notebook advantages, such as Latex notes and headings between cells. Thanks a lot for any advice!
It would be great if you could tell us how you print your graph (what library ?). Ipython provide magic command. For example, if you use matplotlib to plot some figures, just add %matplotlib qt on top of your cell to make the plots appear in a separate window.
See the list of magic command available here.

Force matplotlib to fully render plots in an IPython/Jupyter notebook cell before advancing to next cell

In an IPython/Jupyter notebook I have a situation similar to the following pseudo code:
cell 1:
run some computation
plot several plots (separate figures)
cell 2:
run some computation
plot several plots (separate figures)
This is working nicely, except for one annoyance. When I run both cells sequentially (Shift-enter, Shift-enter), the computation in the second cell starts running before the plots in the first cell are rendered and the plots for both cells are only rendered after the computation for both cells is completed. Just to be clear, the figures for the plots in the first cell are created immediately after the computation in the first cell is completed, but they remain empty until after the computation in the seconds cell is also completed.
This would not be a huge problem except that if there is an uncaught exception in the second cell, which kills the computation for some reason, the plots in the first cell will never be rendered and the figures will remain empty.
I am looking for a way to instruct matplotlib or jupyter (I am not sure where the issue is) at the end of cell 1 - finish rendering all outstanding plots before continuing code execution.
I am using the %matplotlib notebook magic and matplotlib 1.5.3.
Thanks!
For anyone running into this problem, using matplotlib 1.5.3 and jupyter-client 4.3 I am able to force draw using: plt.gcf().canvas.draw().
For rendering multiple figures that were broken due to an exception in some previous cell, I found this to work:
for fig_num in plt.get_fignums():
plt.figure(fig_num).canvas.draw()

savefig loop adds previous plots to figure

I am trying to use a forloop to produce figures for each set of data I have, but while the .show() command produces the correct figure, .savefig() keeps adding the previous plotted values to the new figure.
In my forloop, this is the relevant sample of the code.
import matplotlib.pyplot as plt
plt.plot(X,Y[:,0],'o-')
plt.xlabel('x')
plt.savefig('plot'+str(i)+'.png')
As a comparison, here is the savefig plot and here is that shown by show(). As can be seen, the savefig() plot also plotted the previous result.
You have to close current figure after saving with function
plt.close(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.close
Or you have to clean current figure after saving by plt.clf(): http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clf
I made some beautiful charts before I included plt.clf() to clear the plot each time through the loop.
scatterplot1
scatterplot2
In other words, my previous plots were being added to a single figure as shown in the lots above, within my for loop as well. adding [plt.clf()] to clear the plot each time through the loop fixed this problem being clearing the figure before starting the loop iteration at the top to create a new figure with new plots.
TLDR; I included plt.clf() to clear the plot each time through the loop.

Categories