1

I would like to indicate a relationship between some of my data points in the main subplot and some other subplots using a filled triangular shape. I found that Connection Patch allows you to draw lines/arrows between different axis, but no filled shapes. Since I want a filled triangle, I tried to extract the coordinates (in axis coordinates of the main subplot) of the Patch and draw a Polygon with the same coordinates. The Polygon is then however cut by off by the axes of the subplot. How can I make it visible also between plots?

This is how it currently looks like.

And this is a minimal working example:

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)
frase
  • 13
  • 4

1 Answers1

2

You can set clip_on=False:

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

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you, that is very helpful! How can I now also let the x-axes of the small plots be in the foreground? If I set `zorder=0` the triangle falls behind the axis of the main subplot but still covers the small subplots x-axes. – frase Jul 11 '18 at 11:44
  • I think you want to set the zorder of the small axes to something higher than the zorder of the big axes. – ImportanceOfBeingErnest Jul 11 '18 at 11:55
  • Assigning ax0a through ax0c with ´zorder = 1´ while having the triangle at ´zorder =0´ does the trick for me. Thanks again! – frase Jul 11 '18 at 13:33