2

I want to create a plot in ; the original range of x values is [0-70], but I wish to zoom in on the interval range of [30-40]. Essentially magnify that range in a separate plot.

2
  • 1
    Do you need two subplots or a single plot with a zoomed inset? Commented Feb 10, 2019 at 15:50
  • Possible duplicate of pyplot zooming in Commented Feb 10, 2019 at 19:15

2 Answers 2

2

You can do two separate plots, like

import matplotlib.pyplot as plt x=[10,20,30,40,50,60,70] #for example y=[1,2,3,4,5,6,7] fig, ax = plt.subplots(1,2) ax[0].plot(x,y) # original plot ax[1].plot(x,y) # second plot ax[1].set_xlim(30,40) # set a limit on x-axis, is like a zoom plt.show() 

And you get

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Please try running your code and see if it works. Fix the typos. pet. It should be set_xlim
thank you, I'm sorry but I clicked on the answer button too fast.
set_xlim(30,40) this is the key .. .thank you . i coldnt find it in the tutorials .
@MosesS: Care to share the tutorials you couldn't find it in?
chill out Bazingaa .
2

Besides the other answer, you might also be interested in knowing how to use insets in the figure to highlight some particular range of curve. Here, the first two values in plt.axes([.2, .5, .3, .3]) define the starting point of your inset figure axis in relative coordinates (0 to 1) and the following two values (.3, .3) defines the x-length and y-length of your inset again. This can be controlled to place the inset at position of interest.

import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(8,6)) x = range(70) plt.plot(x,x) ax2 = plt.axes([.2, .5, .3, .3]) ax2.plot(x,x) ax2.set_xlim(30, 40) ax2.set_title('Zoomed') plt.show() 

enter image description here

8 Comments

Matplotlib has ways of creating insets. No need to place an axes manually in figure coordinates.
@ImportanceOfBeingErnest: From this and this and this, it seems to be the most used approach where the coordinates have been used manually. I would love to see other better ways to put an inset. Are you referring to this?
I'm refering to this and this. In terms of SO answers, this and this.
Ok, I see. Thanks. So your comment was not about using [.2, .5, .3, .3] which is manual work putting coordinates manually, but to using ax.inset_axes instead of plt.axes because in the second the fourth link you shared, values have been entered manually just like in my answer
Well, point is plt.axes() takes figure coordinates. But an inset should be bound to an axes (inset). ax.inset_axes takes axes coordinates, so you place the inset relative to the axes, which makes much more sense. Or in fact any other coordinate system you want to specify.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.