You are having problems due to the way matplotlib displays graphs. By default, the second axis is displayed after the first (they have the same zorder so that they appear in the order in which they were added).
To get what you need, you just need to set up a few things about your axes:
figure() ax1 = plt.subplot(111) ax2 = ax1.twinx() ax2.plot([1, 2, 3], [0.3, 0.2, 0.1], 'r') ax1.plot([1, 2, 3], [1, 2, 3], 'b', label='ax1') ax1.legend(loc=2) ax1.set_zorder(1)
We set the zorder of ax1 above, so it is displayed later, but if we do just that, then the second axes are not visible at all, since they are all drawn under the frame (white background and field) of ax1 . To fix this, we turn off the frame on ax1 (so that we can see ax2 ). However, now we have no background or bounding box. Then we can turn on the frame for ax2 , which gives us the desired effect.
The above method is ad-hoc and is not general, if you want to make sure that the axes are above all the axes, you need to use Figure.ledgend() , which is figure , not axes . Currently, it will not automatically type shortcuts, so you must explicitly pass them in pens and shortcuts:
fig = figure() ax1 = plt.subplot(111) ax2 = ax1.twinx() ln2, = ax2.plot([1, 2, 3], [0.3, 0.2, 0.1], 'r', label='ax2') ln1, = ax1.plot([1, 2, 3], [1, 2, 3], 'b', label='ax1') interesting_lines = [ln1, ln2] fig.legend(*zip(*[(il, il.get_label()) for il in interesting_lines]), loc=2) plt.show()
Notice that this legend is now placed using the shape coordinates.