Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inset zoom of matplotlib plot is marked on the wrong corners

I want to plot a curve and zoom and have an inset which shows a zoom of a specific part of my plot. This is my code, which partly realizes this:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

fig, ax = plt.subplots()
axins = inset_axes(ax, 1,1 , loc=2, bbox_to_anchor=(.08, 0.35),bbox_transform=axfft.figure.transFigure)

x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.plot(x, y)

x1, x2, y1, y2 = 1, 2, .5, 4.5 # specify the limits
axins.set_xlim(x1, x2) # apply the x-limits
axins.set_ylim(y1, y2) # apply the y-limits

plt.xticks(visible=False)
plt.yticks(visible=False)

mark_inset(ax, axins, loc1=2, loc2=3, fc="none", ec="0.5")

This is the output it produces:

enter image description here

MY QUESTION:

How can I make mark_inset put the lines on the right corners of the inset instead of the left ones? The way it currently is, the indicator lines cross my inset and I do not want that.

like image 490
HeinzKurt Avatar asked Sep 07 '25 03:09

HeinzKurt


1 Answers

mark_inset always uses the same corners on both, the inset and the location rectangle. You can set those with the loc1 and loc2 argument.

To prevent the lines crossing the axes, you may use

mark_inset(ax, axins, loc1=1, loc2=3, fc="none", ec="0.5")

enter image description here

like image 189
ImportanceOfBeingErnest Avatar answered Sep 10 '25 00:09

ImportanceOfBeingErnest