I'm plotting a series of lines on a pair of twin (twinx
) axes. I can control which lines are 'on top' of each axis using the zorder
parameter, and I can set which axis (as a whole) is on top using the Axes.set_zorder()
function --- making all of the lines on one axis above/below all of the lines on the other axis. Is there a way to interweave the lines between the two axes?
For example, I want the upper plot (which uses a twin axis) to have the lines stacked in the same was as in the lower plot (which doesn't use a twin axis):
import numpy as np
import matplotlib.pyplot as plt
NUM_PNTS = 100
NUM_LINES = 10
scale = np.linspace(1.0,100.0,num=NUM)[::-1]
lines_left = [ np.random.uniform(low=-scale, high=scale, size=NUM)
for ii in xrange(NUM_LINES) ]
lines_right = [ 50.0 + np.random.uniform(low=-scale, high=scale, size=NUM)
for ii in xrange(NUM_LINES) ]
fig,ax = plt.subplots(figsize=[12,12], nrows=2)
tw = ax[0].twinx()
for ll,lr in zip(lines_left,lines_right):
ax[0].plot(ll, c='b', lw=2.0)
tw.plot(lr, c='r', lw=2.0)
ax[1].plot(ll, c='b', lw=2.0)
ax[1].plot(lr, c='r', lw=2.0)
tw.set_ylim(-150,150)
for axis in ax: axis.set_ylim(-150,150)
I've tried setting the zorder to be interspaced, but on different axes it doesn't work, i.e.
for ii,(ll,lr) in enumerate(lines_left, lines_right):
ax[0].plot(ll, c='b', lw=2.0, zorder=2*ii )
tw.plot(lr, c='b', lw=2.0, zorder=2*ii+1)