简体   繁体   中英

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

In the following example, I want to draw a line connecting the x marker with the upper right corner, of the inset axis. Unfortunately, I cannot rely on the dimensions of the inset axis, since they will change according to the data plotted in there.

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

在此处输入图像描述

Found a solution: ConnectionPatch and transforming axis coordinates.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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