Different x and y scale in zoomed inset, matplotli

2019-03-25 12:38发布

问题:

I am trying to make an inset plot using matplotlib. Currently I have something like the last answer in How to zoomed a portion of image and insert in the same plot in matplotlib

There is a parameter there which determines the zoom factor. However, I want to change the scale between the x and y axes, ie I want to zoom in more on the xaxis. (so in the example, the square would be mapped to a rectangle under the inset map).

How do I achieve this?

Here is a working example:

import pylab as pl
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

x=np.linspace(0,1,100)
y=x**2

ax=pl.subplot(1,1,1)
ax.plot(x,y)

axins = zoomed_inset_axes(ax, 1, loc=2,bbox_to_anchor=(0.2, 0.55),bbox_transform=ax.figure.transFigure) # zoom = 6



axins.plot(x,y)

x1, x2= .4, .6
y1,y2 = x1**2,x2**2
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")
pl.show()

So what I want to do is to be able to change the width and height of the inset separately, without changing the x and y ranges of the inset.

回答1:

For anyone else looking for this, it turns out this can be accomplished by using the inset_axes() method rather than zoomed_inset_axes().

For example:

axins = inset_axes(ax, 1,1 , loc=2,bbox_to_anchor=(0.2, 0.55),bbox_transform=ax.figure.transFigure) # no zoom

The 1,1 section sets the width and height of the inset, respectively. After this, use the xlim() and ylim() to set the extents of the axes without changing the size or shape of the inset box.