在 matplotlib 中绘制不同轴之间的填充形状

Drawing filled shapes between different axes in matplotlib

我想使用实心三角形指示主要子图中的一些数据点与其他一些子图中的数据点之间的关系。 我发现 Connection Patch 允许您在不同轴之间绘制 lines/arrows,但没有填充形状。因为我想要一个实心三角形,所以我尝试提取 Patch 的坐标(在主要子图的轴坐标中)并绘制一个具有相同坐标的多边形。然而,多边形随后被子图的轴切断。我怎样才能让它在地块之间也可见?

This is how it currently looks like.

这是一个最小的工作示例:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gs
import numpy as np

fig, ax = plt.subplots()
ax.axis('off')

grid_t = gs.GridSpec(4,3)
ax0a = fig.add_subplot(grid_t[0:1,0:1])
ax0b = fig.add_subplot(grid_t[0:1,1:2])
ax0c = fig.add_subplot(grid_t[0:1,2:3])
ax1 = fig.add_subplot(grid_t[1:4,:])

xl = ax0a.get_xlim()
yl = ax0a.get_ylim()
ptAl = (xl[0], yl[0])
ptAr = (xl[1], yl[0])

ptD1 = (0,0)
ptD2 = (1,1)
ptD3 = (2,1)

ax1.plot([-1,0,1,2,3],[2,0,1,1,-1],'ko')

from matplotlib.patches import ConnectionPatch

for pts,axs,num in [[ptD1,ax0a,1],[ptD2,ax0b,2],[ptD3,ax0c,3]]:

    con1 = ConnectionPatch(xyA=pts, xyB=ptAl, coordsA="data", coordsB="data",
                          axesA=ax1, axesB=axs,color='grey',shrinkA=0,shrinkB=0)
    ax1.add_artist(con1)
    con2 = ConnectionPatch(xyA=pts, xyB=ptAr, coordsA="data", coordsB="data",
                          axesA=ax1, axesB=axs,color='grey',shrinkA=0,shrinkB=0)
    ax1.add_artist(con2)

    line2=con2.get_path().vertices
    line1=con1.get_path().vertices

    zoomcoords = sorted(np.concatenate((line1[1:],line2)),key=lambda x: x[0])
    triangle = plt.Polygon(zoomcoords,ec='black',fc='red',zorder=100)
    ax1.add_artist(triangle)

你可以设置clip_on=False:

triangle = plt.Polygon(zoomcoords,ec='black',fc='red',zorder=100, clip_on=False)