gpt4 book ai didi

python - 在 Matplotlib 中的两个图之间画线

转载 作者:IT老高 更新时间:2023-10-28 20:31:06 25 4
gpt4 key购买 nike

我正在使用 Matplotlib 绘制两个子图,基本上如下:

subplot(211); imshow(a); scatter(..., ...)
subplot(212); imshow(b); scatter(..., ...)

我可以在这两个子图之间画线吗?我该怎么做?

最佳答案

其他答案的解决方案在许多情况下都不是最优的(因为它们只有在计算点后没有对绘图进行任何更改时才有效)。

更好的解决方案是使用专门设计的 ConnectionPatch :

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

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

x,y = np.random.rand(100),np.random.rand(100)

ax1.plot(x,y,'ko')
ax2.plot(x,y,'ko')

i = 10
xy = (x[i],y[i])
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
axesA=ax2, axesB=ax1, color="red")
ax2.add_artist(con)

ax1.plot(x[i],y[i],'ro',markersize=10)
ax2.plot(x[i],y[i],'ro',markersize=10)


plt.show()

enter image description here

关于python - 在 Matplotlib 中的两个图之间画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17543359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com