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

2024-09-28 22:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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")

这是它产生的输出:

enter image description here

我的问题:

如何使mark_inset将线条放在插图的右角而不是左角?目前的情况是,指示线穿过我的插图,我不想这样。在


Tags: thefromimportasnppltaxmpl
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:28

mark_inset始终在插入和位置矩形上使用相同的角点。您可以用loc1loc2参数设置这些参数。在

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

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

enter image description here

相关问题 更多 >