matplotlib.dates date within a loop

Hi, I get unexpected behaviour using the script below. xticks only appear in
the 4th subplot. If the lines marked ### are moved out of the loop (and edited
so that they are constants), then xticks do appear on each subplot. Is this a
bug or am I missing something?

I am using matplotlib 1.1.0svn.

Many thanks, Evan

import numpy as np
import datetime as dt
import matplotlib.dates as dates
import matplotlib.pyplot as plt

months = dates.MonthLocator(range(1,13), bymonthday=1, interval=1)
monthsFmt = dates.DateFormatter("%b %Y")
deltad = 2
delta = dt.timedelta(days=deltad)

plt.close('all')
fig1 = plt.figure(1)
for i in range(4):
    date_str = dt.date(i+2,11,1) ###
    date_end = dt.date(i+3,3,1) ###
    date_range = dates.drange(date_str,date_end,delta) ###
    ax = fig1.add_subplot(2,2,i+1)
    ax.plot_date(date_range,np.sin(date_range))
    ax.xaxis.set_major_locator(months)
    ax.xaxis.set_major_formatter(monthsFmt)

plt.show()

Evan Mason, on 2011-03-16 15:27, wrote:

Hi, I get unexpected behaviour using the script below. xticks only appear in
the 4th subplot. If the lines marked ### are moved out of the loop (and edited
so that they are constants), then xticks do appear on each subplot. Is this a
bug or am I missing something?

I am using matplotlib 1.1.0svn.

Hi Evan,

What you're seeing is the result of each instance of locator
belonging to one and only one axis (namely months.axis), and
every time you call ax.xaxis_major_locator, you're moving it to
the newest subplot.

Just move the 'months = ...' line inside the for-loop, and you'll
be all set.

best,

···

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