Search code examples
pythonmatplotlibdrawlinessubplot

Improving accuracy of drawn lines using ConnectionPatch? (Matplotlib)


I plotted point correspondences in two subplots. Between these correspondences I want to draw lines that start and end exactly in the points. I have the problem that ConnectionPatch of matplotlib only starts next to the given coordinates what looks silly.

enter image description here

The data is of the form:

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

My code:

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

Is the function ConnectionPatch the problem? Or is there a way to make the drawings of this function more accurate? If not is there a possible solution without using this function?


Solution

  • ConnectionPatch itself works fine.
    I think you confused the axes in which the points are situated. It should probably be

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

    Complete example:

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

    enter image description here