Matplotlib's inset_locator with inverted axes
Asked Answered
F

1

5

I want to make a standard inset into my plot. But I don't get how to use the inset_locator. Here is my code:

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.axes_grid1.inset_locator import inset_axes, mark_inset

x = np.linspace(0, 2)
plt.plot(x, np.sin(x))

ax = plt.gca()
ax.invert_yaxis()
axins = inset_axes(ax, width='40%', height='30%', loc='lower left')
x_in = np.linspace(1.25, 1.75)
axins.plot(x_in, np.sin(x_in))
axins.invert_yaxis()
mark_inset(ax, axins, loc1=2, loc2=4)
plt.show()

And the result is:enter image description here

Apparently it the edges connect the wrong corners. How do I get them right, when my axis goes from maximum to minimum?

Fairspoken answered 25/2, 2018 at 18:18 Comment(0)
P
10

Unfortunately the mark_inset cannot cope with inverted axes. So you need to set the locations of the connectors manually.

patch, pp1,pp2 = mark_inset(ax, axins, loc1=1,loc2=1)
pp1.loc1 = 1
pp1.loc2 = 4
pp2.loc1 = 4
pp2.loc2 = 1

enter image description here

Polyphony answered 26/2, 2018 at 11:52 Comment(1)
Note for anyone running this interactively and confused why changing the locations doesn't do anything- you may need to add a plt.draw() after setting them to redraw the lines.Snore

© 2022 - 2024 — McMap. All rights reserved.