Hi. It's me again, asking about dates again.
is there any easy way to a collection using dates on the X axes? I've taken the collection example from the website and adopted it so that there is a use_dates flag. Set it to False and spirals demo appears. Set it to True and I get this error:
Traceback (most recent call last):
File "mpl_collection2.py", line 51, in <module>
ax.add_collection(col, autolim=True)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/axes.py", line 1312, in add_collection
self.update_datalim(collection.get_datalim(self.transData))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/collections.py", line 144, in get_datalim
offsets = transOffset.transform_non_affine(offsets)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/transforms.py", line 1914, in transform_non_affine
self._a.transform(points))
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/transforms.py", line 1408, in transform
return affine_transform(points, mtx)
ValueError: Invalid vertices array.
The code is below.
Thanks!
···
=========================
import matplotlib
import matplotlib.pyplot
from matplotlib import collections, transforms
from matplotlib.colors import colorConverter
import numpy as N
import datetime
use_dates = False
nverts = 50
npts = 100
# Make some spirals
r = N.array(range(nverts))
theta = N.array(range(nverts)) * (2*N.pi)/(nverts-1)
xx = r * N.sin(theta)
yy = r * N.cos(theta)
spiral = zip(xx,yy)
# Make some offsets
rs = N.random.RandomState([12345678])
if not use_dates:
xo = [i for i in range(0,100)]
else:
xo = [datetime.date(1990,1,1)+datetime.timedelta(10)*i for i in range(0,100)] # new version
yo = rs.randn(npts)
xyo = zip(xo, yo)
colors = [colorConverter.to_rgba(c) for c in ('r','g','b','c','y','m','k')]
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(1,1,1)
if use_dates:
import matplotlib.dates as mdates
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
yearsFmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFmt)
ax.set_xlim(datetime.date(1990,1,1),datetime.date(1992,12,31))
col = collections.LineCollection([spiral], offsets=xyo, transOffset=ax.transData)
trans = fig.dpi_scale_trans + transforms.Affine2D().scale(1.0/72.0)
col.set_transform(trans) # the points to pixels transform
ax.add_collection(col, autolim=True)
col.set_color(colors)
ax.autoscale_view()
ax.set_title('LineCollection using offsets')
matplotlib.pyplot.show()