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.
Asked
Active
Viewed 2,252 times
2
-
1Do you need two subplots or a single plot with a zoomed inset? – Sheldore Feb 10 '19 at 15:50
-
Possible duplicate of [pyplot zooming in](https://stackoverflow.com/questions/11400579/pyplot-zooming-in) – Thomas Kühn Feb 10 '19 at 19:15
2 Answers
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

Alessandro Peca
- 873
- 1
- 15
- 40
-
1Please try running your code and see if it works. Fix the typos. `pet`. It should be `set_xlim` – Sheldore Feb 10 '19 at 16:20
-
thank you, I'm sorry but I clicked on the answer button too fast. – Alessandro Peca Feb 10 '19 at 16:23
-
set_xlim(30,40) this is the key .. .thank you . i coldnt find it in the tutorials . – Omid Feb 10 '19 at 16:50
-
-
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()

Sheldore
- 37,862
- 7
- 57
- 71
-
Matplotlib has ways of creating insets. No need to place an axes manually in figure coordinates. – ImportanceOfBeingErnest Feb 10 '19 at 18:48
-
@ImportanceOfBeingErnest: From [this](https://stackoverflow.com/questions/17458580/embedding-small-plots-inside-subplots-in-matplotlib) and [this](https://matplotlib.org/examples/pylab_examples/axes_demo.html) and [this](https://stackoverflow.com/questions/21001088/how-to-add-different-graphs-as-an-inset-in-another-python-graph), 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](https://matplotlib.org/gallery/axes_grid1/inset_locator_demo.html)? – Sheldore Feb 10 '19 at 18:53
-
I'm refering to [this](https://matplotlib.org/gallery/axes_grid1/inset_locator_demo.html) and [this](https://matplotlib.org/gallery/subplots_axes_and_figures/zoom_inset_axes.html). In terms of SO answers, [this](https://stackoverflow.com/a/34862213/4124317) and [this](https://stackoverflow.com/a/53667739/4124317). – ImportanceOfBeingErnest Feb 10 '19 at 19:00
-
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 – Sheldore Feb 10 '19 at 19:03
-
Well, point is `plt.axes()` takes figure coordinates. But an inset should be bound to an axes (*in*set). `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. – ImportanceOfBeingErnest Feb 10 '19 at 19:08
-
-
Though let me mention that personally I'm a big fan of [the axis_grid1 solution](https://matplotlib.org/gallery/axes_grid1/inset_locator_demo.html) which does not require any coordinates at all. – ImportanceOfBeingErnest Feb 10 '19 at 19:15
-
@ImportanceOfBeingErnest: I also find this to be the best solution. It is easy to control a single percentage rather than specifying the extents – Sheldore Feb 10 '19 at 19:17