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

时间:2018-07-10 14:59:26

标签: python matplotlib zoom multiple-axes

我想用填充的三角形表示我在主子图中的某些数据点与其他子图之间的关系。 我发现Connection 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)

1 个答案:

答案 0 :(得分:1)

您可以设置clip_on=False

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

enter image description here

相关问题