I want to create a plot in matplotlib; 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.
- 1Do you need two subplots or a single plot with a zoomed inset?Sheldore– Sheldore2019-02-10 15:50:48 +00:00Commented Feb 10, 2019 at 15:50
- Possible duplicate of pyplot zooming inThomas Kühn– Thomas Kühn2019-02-10 19:15:33 +00:00Commented Feb 10, 2019 at 19:15
Add a comment |
2 Answers
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
5 Comments
Sheldore
Please try running your code and see if it works. Fix the typos.
pet. It should be set_xlimAlessandro Peca
thank you, I'm sorry but I clicked on the answer button too fast.
Omid
set_xlim(30,40) this is the key .. .thank you . i coldnt find it in the tutorials .
Sheldore
@MosesS: Care to share the tutorials you couldn't find it in?
Omid
chill out Bazingaa .
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() 8 Comments
ImportanceOfBeingErnest
Matplotlib has ways of creating insets. No need to place an axes manually in figure coordinates.
ImportanceOfBeingErnest
Sheldore
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 answerImportanceOfBeingErnest
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. |

