连接子图之间的点以绘制一条线。

huangapple go评论55阅读模式
英文:

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

连接子图之间的点以绘制一条线。

huangapple
  • 本文由 发表于 2023年3月7日 05:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定