plot_date and legend

I'm noticing strange behavior with the legend function when used in combination with matplotlib. If I do the following:

import datetime
from pylab import *
date1 = datetime.date( 1952, 1, 1 )
date2 = datetime.date( 2004, 4, 12 )
delta = datetime.timedelta(days=100)
dates = drange(date1, date2, delta)
s1 = rand(len(dates))
plot_date(dates, s1,'r.')
hold(True)
s2 = rand(len(dates))
plot_date(dates, s2,'bo')
legend(('s1','s2'))

The resulting legend shows the symbols twice (two little red dots and two blue ones). Does anyone else get this, and if so, do you know what the problem is?

--Mike

···

------------------------------------------------------
Michael Hearne
mhearne@...924...
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------

Contents of matplotlib.__version__"
'0.98pre'

My Matplotlib is from the SciPy SuperPack for Mac OS X from http://macinscience.org/?page_id=6

···

On May 1, 2008, at 3:32 PM, Michael Hearne wrote:

I'm noticing strange behavior with the legend function when used in combination with matplotlib. If I do the following:

import datetime
from pylab import *
date1 = datetime.date( 1952, 1, 1 )
date2 = datetime.date( 2004, 4, 12 )
delta = datetime.timedelta(days=100)
dates = drange(date1, date2, delta)
s1 = rand(len(dates))
plot_date(dates, s1,'r.')
hold(True)
s2 = rand(len(dates))
plot_date(dates, s2,'bo')
legend(('s1','s2'))

The resulting legend shows the symbols twice (two little red dots and two blue ones). Does anyone else get this, and if so, do you know what the problem is?

--Mike

------------------------------------------------------
Michael Hearne
mhearne@...924...
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------

------------------------------------------------------
Michael Hearne
mhearne@...924...
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------

You can tell the legend how many points to draw using the numpoints
kwarg. See the docs at
http://matplotlib.sourceforge.net/matplotlib.legend.html#Legend

Check out the example code below -- in addition tomaking the numpoints
setting, it also fixes the tick format overlap problem

import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mpldates
import matplotlib.ticker as ticker

date1 = datetime.date( 1952, 1, 1 )
date2 = datetime.date( 2004, 4, 12 )
delta = datetime.timedelta(days=100)
dates = mpldates.drange(date1, date2, delta)
s1 = np.random.rand(len(dates))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot_date(dates, s1,'r.')
ax.hold(True)
s2 = np.random.rand(len(dates))
ax.plot_date(dates, s2,'bo')
ax.legend(('s1','s2'), numpoints=1)

# only write ticklabels on the decades
def fmtticks(x, pos=None):
    dt = mpldates.num2date(x)
    if dt.year%10: return ''
    return dt.strftime('%Y')

# this fizes yhe tick labels
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmtticks))

# this fixes the toolbar x coord
ax.fmt_xdata = mpldates.DateFormatter('%Y-%m-%d')

# this rotates the ticklabels to help with overlapping
fig.autofmt_xdate()
plt.show()

JDH

···

On Thu, May 1, 2008 at 4:32 PM, Michael Hearne <mhearne@...924...> wrote:

legend(('s1','s2'))

The resulting legend shows the symbols twice (two little red dots and
two blue ones). Does anyone else get this, and if so, do you know
what the problem is?