Control twinx series zorder: ax2 series behind ax1 series, or place ax2 on left, ax1 on right

I am trying to plot multiple series on one axes and a bar chart on a shared x,
new y axes. I'm using twinx. The following test code works, but I need to render
the bar plot *behind* the line plots. Can I somehow control zorder globally
within a figure?

Alternatively, I could make the bar chart the first plot/axes ax1, but the
position for its y-axis must be on the right, the line chart's y-axis on the
left. Can this be controlled?

Thanks.

test.py:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

width_bar = 0.4
width_line = 6

for i in range(0,1):
    data = np.loadtxt(open('test.csv','r'), delimiter=',',
                     dtype={'names': ('x','y1','y2','count'),
                            'formats': ('f4', 'i4', 'i4', 'i4', 'i4')},
                     usecols=(1,3,4,7))
    x,y1,y2,count = data['x'],data['y1'],data['y2'],data['count']
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(x, y1, linewidth=width_line, zorder=3)
    ax1.plot(x, y2, linewidth=width_line, zorder=2)
    ax2 = ax1.twinx()
    ax2.bar(x, count, width=width_bar, color='0.9', zorder=1)
    plt.show()

test.csv:

1,3.0,3.2,6,0,-10,29,8
1,3.5,3.5,20,12,-4,55,29
1,4.0,4.0,51,29,15,97,71
1,4.5,4.5,82,62,37,136,143
1,5.0,5.0,130,102,69,197,154
1,5.5,5.5,186,147,115,275,164
1,6.0,6.0,256,202,159,382,187
1,6.5,6.5,351,265,236,522,183
1,7.0,7.0,446,340,308,646,192
1,7.5,7.5,562,426,376,826,184
1,8.0,8.0,654,526,437,934,193
1,8.5,8.5,797,648,553,1080,190
1,9.0,9.0,940,765,648,1271,173
1,9.5,9.5,1111,910,782,1457,180
1,10.0,10.0,1240,1040,919,1564,199
1,10.5,10.5,1333,1175,1011,1655,209
1,11.0,11.0,1449,1285,1112,1775,210
1,11.5,11.5,1458,1366,1120,1793,173
1,12.0,12.0,1449,1427,1089,1799,156
1,12.5,12.5,1441,1460,1073,1809,136
1,13.0,13.0,1458,1483,1100,1803,174
1,13.5,13.5,1446,1491,1080,1809,134
1,14.0,14.0,1447,1497,1081,1814,139
1,14.5,14.5,1424,1498,1046,1810,112
1,15.0,15.0,1433,1500,1065,1817,85
1,15.5,15.5,1427,1500,1048,1819,66
1,16.0,15.9,1428,1500,1036,1817,41
1,16.5,16.5,1383,1500,990,1799,29
1,17.0,17.0,1379,1500,935,1820,29
1,17.5,17.5,1352,1500,899,1817,16
1,18.0,18.0,1347,1500,861,1825,9
1,18.5,18.4,1464,1500,1130,1818,4
1,19.0,18.9,1449,1500,1065,1819,3

Jeff Kowalczyk, on 2011-02-25 23:20, wrote:

I am trying to plot multiple series on one axes and a bar chart on a shared x,
new y axes. I'm using twinx. The following test code works, but I need to render
the bar plot *behind* the line plots. Can I somehow control zorder globally
within a figure?

Hi Jeff,

add these two lines before your plt.show()

  ax1.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2
  ax1.patch.set_visible(False) # hide the 'canvas'

Alternatively, I could make the bar chart the first plot/axes ax1, but the
position for its y-axis must be on the right, the line chart's y-axis on the
left. Can this be controlled?

  ax1.yaxis.tick_right()
  ax2.yaxis.tick_left()

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7