0

When I tried to draw an arrow, the pointing end got blocked by other axes, do you know how to make that part visible?

It'll be great if you could edit on top of the sample code I have here.

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

grid_data = np.array([[[-0.1779874 , -0.90335705, -0.31157705,  0.77770067],
        [ 0.93698288,  0.79215241,  0.10155888,  0.96101718],
        [ 0.72994894, -0.83939131,  0.24713443,  0.74839211],
        [ 0.10039462, -0.95778299,  0.43554077,  0.61927077]],

       [[ 0.52294259, -0.0247383 ,  0.23717517, -0.0857769 ],
        [-0.43539246,  0.28503173, -0.39443502, -0.1478289 ],
        [-0.2327904 , -0.08339054,  0.33072907,  0.74634504],
        [-0.524284  , -0.72919194, -0.61543159,  0.17086563]]])
fig, axes = plt.subplots(nrows = 2, ncols = 2, figsize = (2.6, 2.8))
for i in range(2):
    c = axes[0,i].pcolormesh(grid_data[i], cmap = 'RdBu_r', vmin = -1., vmax = 1.)
xyA = (0.5,0)
xyB = (0.5,.5)
coordsA = "axes fraction"
coordsB = "axes fraction"
con = ConnectionPatch(xyA = xyA, xyB = xyB, coordsA=coordsA, coordsB=coordsB,
                      axesA=axes[0,0], axesB=axes[0,1],
                      arrowstyle="->", shrinkB=5, linewidth = 5, edgecolor='b', clip_on=False)
axes[0,0].add_artist(con)
cb_ax = fig.add_axes([0.91, 0.13, 0.01, 0.75])
fig.colorbar(c, cax = cb_ax)
plt.draw()
plt.show()

enter image description here

lw1.at
  • 1,528
  • 11
  • 25
Jason
  • 3,166
  • 3
  • 20
  • 37
  • did you see https://github.com/matplotlib/matplotlib/issues/8744 – Sam Mason Sep 09 '19 at 17:38
  • 2
    You need to add the ConnectionPatch to the top axes, in this case `axes[0,1].add_artist(con)`. From next matplotlib version on you can also add it to the figure via `fig.add_artist(con)`. – ImportanceOfBeingErnest Sep 09 '19 at 17:41
  • 1
    Correct answer goes both to ImportanceOfBeingErnest and lw1.at, two different ways depends on which is more intuitive to yourself. But it looks like ImportanceOfBeingErnest likes to give the Accepted Answer to more other people :P – Jason Sep 09 '19 at 17:50

1 Answers1

2

Thanks to @Sam Mason for the pointer. One simple change to fix this would be to set the zorder to the reverse drawing order

for i in range(2):
    c = axes[0,i].pcolormesh(grid_data[i], cmap = 'RdBu_r', vmin = -1., vmax = 1.)
    axes[0,i].set_zorder(2-i)
lw1.at
  • 1,528
  • 11
  • 25