How do I show plot zoomed in? - python

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?

Related

Zoom in a polar chart matplotlib

**Note: My code is at the end of the question.
I want to recreate this chart in matplotlib:
I have come a long way of making something similar but I am stuck in the last part, which is to zoom in the plot to remove all the white space. So basically, I want to zoom in to the red box:
Note that the grid will be removed on the actual chart. Displaying it now to explain what I would like done.
Is that possible? My code is here:
https://github.com/Curbal-Data-Labs/Matplotlib-Labs/blob/master/Polar%20charts/Polar%20bar%20chart.ipynb

Inscribing Plot window in the console and do not want the plot window popping up separately

I am facing couple of issues. First, I wanted all the plots in a separate window. For this, I successfully changed the settings and I got the separate window. The problem is, I got all the plots in same figures, which is bad. Second issue is, how do I inscribe window pan to the Ipconsole? I donot want a separate window. I want this window inside the console?
For the first issue, you can have your plots in different figures by using figure this way:
import matplotlib.pyplot as plt
plt.figure()
# Plot your first graph(s)
plt.figure()
# Plot your other graph(s)
plt.show()
Each time you call figure, a new window is created. For more information on figure, you can check the doc

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.

Show new matplotlib graph further down Jupyter notebook

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.

Save images of a matplotlib figure at different coordinates?

I want to create a big figure using matplotlib, and then save a few parts of it at different specific coordinates (so manual zooming after plt.show() is not an option - there is no guarantee I can zoom to some precise coordinates - or is there?). The picture is fairly large, so I don't want to generate it all over again and again, specifying xlim and ylim every time before plotting. Is there any way to change axes limits after the figure is created? And I am not using an ipython console, but I need to use it in a script.
There is no problem with using xlim and ylim here. Take the following example:
import matplotlib.pyplot as plt
plt.plot(range(20))
plt.savefig("1.png")
plt.xlim(0,10)
plt.savefig("2.png")
plt.xlim(0,30)
plt.savefig("3.png")
Here a diagonal line is plotted, then we zoom into the first half of the line, then we zoom back out. At each stage a new png file is created. There is no need for redrawing here.

Categories