matplotlib 图的插入缩放标记在错误的角上

问题描述 投票:0回答:2

我想绘制一条曲线并进行缩放,并有一个插图显示绘图特定部分的缩放。这是我的代码,部分实现了这一点:

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
将线条放在插图的右角而不是左角?目前的情况是,指标线穿过我的插图,我不希望这样。

python matplotlib plot
2个回答
5
投票

mark_inset
始终在插图和位置矩形上使用相同的角。您可以使用
loc1
loc2
参数进行设置。

为了防止线交叉轴,您可以使用

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


0
投票

我知道这个问题已经有近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")

上面的输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.