Matplotlib: End a line above another axis panel

There are two panes of axes, with a small (in gray) inside a large (white).

In the following code, I expect the blue line to be on the top panel of the gray little axis, because the blue line is set to ridiculously high zorder. But this is not so.

Changing the zorderpatch of a small panel is not affected.

If I set the background of the small panel to transparent (or make it invisible), then the blue line will be unlocked, but this solution is not satisfactory, as there may be situations in which it is REQUIRED that the background of the small panel be opaque.

Or maybe such a requirement is not achievable in a simple way, if the implementation of the string zordermakes sense only within the same axes?

f = figure()

ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=1)
ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=2)
ax2.patch.set_facecolor('gray')
ax2.patch.set_zorder(-9999999)
ax1.plot([0,1], [0,1], zorder=99999999, color='blue')
ax2.plot([0,1], [0,3], zorder=-99999, color='red')

# New Edit:
# To make the problem more to the point, what if someone
# also wants the background of the big panel to be green
# (with the following command)?  See the second figure.
ax1.patch.set_facecolor('green')
# This seems to mean that the small panel really has to
# somehow "insert" into the z-space between the big panel
# and the blue line.

Figure generated by the following code New image

+4
source share
2 answers

I think we just need to compromise, so the solution I came up with is to add a third layer of axes, which is transparent, and this is the one who really draws the blue line.

Of course, we need to further refine the first level ( ax1) to suppress redundant elements (for example, it ax1also has axis labels and ticks by default , they are simply hidden below).

f = figure()

ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=0)
ax1.patch.set_facecolor('green')

ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=1)
ax2.patch.set_facecolor('gray')

ax2.plot([0,1], [0,3], color='red')

ax3 = f.add_axes([0.1,0.1,0.8,0.8], zorder=2)
ax3.patch.set_alpha(0)
ax3.plot([0,1], [0,1], color='blue')

enter image description here

+2
source

, ( zorder ), "" () () , . ...

f.set_facecolor('white')

ax2 = f.add_axes([0.3,0.2,0.5,0.4], zorder=1)
ax2.patch.set_facecolor('gray')
ax2.plot([0,1], [0,3], color='red')

ax1 = f.add_axes([0.1,0.1,0.8,0.8], zorder=2)
ax1.plot([0,1], [0,1], color='blue')
ax1.patch.set_alpha(0.0)#make background transparent

enter image description here

+2

All Articles