plotting collections on an X date axes

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()

Simson Garfinkel wrote:

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:

Yes, it looks like a bug in the handling of units in the Collection base class; unit conversion is done at drawing time, but needs either to be done earlier, or to be done independently in the get_datalim method.

Maybe one of the units-support experts will pick this up and fix it. I can't do more now.

Eric

···

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()

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users