繁体   English   中英

在一个轴的坐标与 matplotlib 中插入轴的角之间画一条线

[英]Draw a line between coordinate in one axis to the corner of an inset axis in matplotlib

在下面的示例中,我想绘制一条连接 x 标记与插入轴右上角的线。 不幸的是,我不能依赖插图轴的尺寸,因为它们会根据其中绘制的数据而变化。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(1)
ax.axis([0,2,0,2])
ax.plot(1.5,1.5, "x")

axins = ax.inset_axes([0.1, 0.1, 0.4, 0.3])
#various things happen, that may change the shape of axins

plt.show()

在此处输入图像描述

找到了解决方案:ConnectionPatch 和转换轴坐标。

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch

fig, ax = plt.subplots(1)
ax.axis([0,2,0,2])
ax.plot(1.5,1.5, "x")

axins = ax.inset_axes([0.1, 0.1, 0.4, 0.3])
#various things happen, that may change the shape of axins

con = ConnectionPatch(xyA=(1.5,1.5), coordsA=ax.transData,
                      xyB=(1,1), coordsB=axins.transAxes)
fig.add_artist(con)

plt.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM