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.

Mario
  • 1,631
  • 2
  • 21
  • 51
Omid
  • 57
  • 7

2 Answers2

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

Alessandro Peca
  • 873
  • 1
  • 15
  • 40
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

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
  • Alles klar :) :) – Sheldore Feb 10 '19 at 19:09
  • 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