Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different x and y scale in zoomed inset, matplotlib

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.

like image 716
Jonathan Lindgren Avatar asked Jun 04 '14 10:06

Jonathan Lindgren


People also ask

How do I change the X and Y scale in MatPlotLib?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do you zoomed a portion of image and insert in the same plot in MatPlotLib?

How to zoom a portion of an image and insert in the same plot in Matplotlib? Create x and y points, using numpy. To zoom a part of an image, we can make data for x and y points, in that range. Plot x and y points (Step 1), using the plot() method with lw=2, color red and label.

How do I fix scale in MatPlotLib?

You can use the matplotlib. pyplot module's locator_params() method to change the axis scale. You can adjust the x-axis and y-axis at the same time with the code plt. locator_params(nbins = 10).

How do I scale the X-axis in MatPlotLib?

Import matplotlib. To set x-axis scale to log, use xscale() function and pass log to it. To plot the graph, use plot() function. To set the limits of the x-axis, use xlim() function and pass max and min value to it.


1 Answers

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.

like image 146
Thursdays Coming Avatar answered Oct 04 '22 01:10

Thursdays Coming