使用 ConnectionPatch 提高绘制线条的准确性? (Matplotlib)

Improving accuracy of drawn lines using ConnectionPatch? (Matplotlib)

我在两个子图中绘制了点对应关系。在这些对应关系之间,我想画出恰好在点中开始和结束的线。 我遇到的问题是 ConnectionPatch of matplotlib 只在给定坐标旁边开始,看起来很傻。

数据的格式为:

pts1:
1823.63 464.198
1079.02 473.459
1078.44 481.124
1066.56 487.062
1073.3  516.349
1089.75 527.747
1096.95 571.923
1216.69 572.146
1143.79 586.804
1632.08 598.465
1266.59 600.856

pts2:
1825.54 466.379
1084.3  477.044
1083.53 485.376
1070.61 489.98
1076.97 519.565
1094.11 529.739
1100.92 574.527
1221.61 574.427
1148.73 589.611
1634.8  600.363
1271.47 603.339

我的代码:

fig = plt.figure(2,figsize=(20, 20))

ax1 = fig.add_subplot(121)
ax1.plot(pts1[:,0],pts1[:,1], 'c+', ms = 7)
ax1.axis('off')

ax2 = fig.add_subplot(122)
ax2.plot(pts2[:,0],pts2[:,1], '.', color = '#DC189B', ms = 7)
ax2.axis('off')

for i in range(len(pts1)):
    xy1 = (pts1[i,0],pts1[i,1])
    xy2 = (pts2[i,0],pts2[i,1])
    con = ConnectionPatch(xyA=xy1, xyB=xy2, coordsA="data", coordsB="data",
                      axesA=ax2, axesB=ax1, color='#53F242')
    ax2.add_artist(con)

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

是函数ConnectionPatch的问题吗?或者有没有办法让这个函数的图纸更准确?如果不是,是否有不使用此功能的可能解决方案?

ConnectionPatch 本身工作正常。
我认为您混淆了点所在的轴。应该是

con = ConnectionPatch(xyA=xy2, xyB=xy1, coordsA="data", coordsB="data",
                  axesA=ax2, axesB=ax1, color='#53F242')

完整示例:

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

pts1 = np.array([
[1823.63, 464.198],
[1079.02, 473.459],
[1078.44, 481.124],
[1066.56, 487.062],
[1073.3 , 516.349],
[1089.75, 527.747],
[1096.95, 571.923],
[1216.69, 572.146],
[1143.79, 586.804],
[1632.08, 598.465],
[1266.59, 600.856]])

pts2 = np.array([
[1825.54, 466.379],
[1084.3 , 477.044],
[1083.53, 485.376],
[1070.61, 489.98 ],
[1076.97, 519.565],
[1094.11, 529.739],
[1100.92, 574.527],
[1221.61, 574.427],
[1148.73, 589.611],
[1634.8 , 600.363],
[1271.47, 603.339]])

fig = plt.figure(2,figsize=(20, 20))

ax1 = fig.add_subplot(121)
ax1.plot(pts1[:,0],pts1[:,1], 'c+', ms = 7)
ax1.axis('off')

ax2 = fig.add_subplot(122)
ax2.plot(pts2[:,0],pts2[:,1], '.', color = '#DC189B', ms = 7)
ax2.axis('off')

for i in range(len(pts1)):
    xy1 = (pts1[i,0],pts1[i,1])
    xy2 = (pts2[i,0],pts2[i,1])
    con = ConnectionPatch(xyA=xy2, xyB=xy1, coordsA="data", coordsB="data",
                      axesA=ax2, axesB=ax1, color='#53F242')
    ax2.add_artist(con)

plt.subplots_adjust(wspace=0, hspace=0)
plt.show()