英文:
Draw a line to connect points between subfigures
问题
使用matplotlib的新subfigure - 不是subplot - 而是subfigure功能。我想要在两个子图之间绘制一条线。根据我的实验,我不认为连接补丁(对于子图(subplots)有效)适用于这种情况。有人知道是否可能吗?
英文:
With matplotlib's new subfigure - not subplot - but subfigure feature. I would like to draw a line between two subfigures. From my experimentation, I don't believe connection patch (which works for subplots) works with this. Does anyone know if it possible?
答案1
得分: 1
ConnectionPatch
可以与子图一起使用,但正如@JodyKlymak指出的,应该通过set_in_layout(False)
将该图形从布局计算中移除。
这里有一个示例,连接第一个子图的ax1a
上的点(4, 8)与第二个子图的ax2
上的点(2, 5):
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
fig = plt.figure(layout='constrained')
sf1, sf2 = fig.subfigures(1, 2, wspace=0.07)
ax1a, ax1b = sf1.subplots(2, 1)
ax1a.scatter(4, 8)
ax1b.scatter(10, 15)
ax2 = sf2.subplots()
ax2.scatter(2, 5)
conn = ConnectionPatch(
xyA=(4, 8), coordsA='data', axesA=ax1a,
xyB=(2, 5), coordsB='data', axesB=ax2,
color='red',
)
ax2.add_artist(conn)
conn.set_in_layout(False) # 从布局计算中移除
plt.show()
英文:
ConnectionPatch
works fine with subfigures, but as @JodyKlymak points out, the patch should be removed from layout calculations via set_in_layout(False)
.
Here is an example connecting (4, 8) on the first subfigure's ax1a
with (2, 5) on the second subfigure's ax2
:
import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
fig = plt.figure(layout='constrained')
sf1, sf2 = fig.subfigures(1, 2, wspace=0.07)
ax1a, ax1b = sf1.subplots(2, 1)
ax1a.scatter(4, 8)
ax1b.scatter(10, 15)
ax2 = sf2.subplots()
ax2.scatter(2, 5)
conn = ConnectionPatch(
xyA=(4, 8), coordsA='data', axesA=ax1a,
xyB=(2, 5), coordsB='data', axesB=ax2,
color='red',
)
ax2.add_artist(conn)
conn.set_in_layout(False) # remove from layout calculations
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论