我想绘制一条曲线并进行缩放,并有一个插图显示绘图特定部分的缩放。这是我的代码,部分实现了这一点:
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")
这是它产生的输出:
我的问题:
如何使
mark_inset
将线条放在插图的右角而不是左角?目前的情况是,指标线穿过我的插图,我不希望这样。
我知道这个问题已经有近7年历史了,但这是我过去使用过的解决方案,它有点hacky,但如果你想继续使用
mark_inset
,它是有效的。您可以简单地交换插图的 y 限制并绘制 y
数据的负变换。
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=(.18, 0.8),bbox_transform=ax.figure.transFigure)
x = np.linspace(0, 3, 100)
y = x**2
ax.plot(x, y)
axins.set_xlim(x1, x2) # apply the x-limits
axins.set_ylim(y2, y1) # apply the y-limits
x1, x2, y1, y2 = 1, 2, .5, 4.5 # specify the limits
axins.plot(x, -y + y2 + y1)
plt.xticks(visible=False)
plt.yticks(visible=False)
mark_inset(ax, axins, loc1=4, loc2=3, fc="none", ec="0.5")
上面的输出: