On Thu, Jun 17, 2010 at 11:39 AM, Bill Eaton <e...@aeroantenna.com> wrote: > I'm struggling to figure out how to format my data so that I can use dates > as x-data. <SNIP> > I tried to import my own data. It looks like > 2005-03-04,0.923115796 > 2005-03-05,0.915828724 > 2005-03-06,0.442521474 > 2005-03-07,0.997096213 > 2005-03-08,0.867752118 > And to import, I use recarray > myarray = np.loadtxt(fullfile, dtype=[('date', '|O4'), ('ydata', > 'float')], delimiter = ',') > rx = myarray.view(np.recarray) > > Data imports fine. But when I go to plot, I get the following error > ValueError: setting an array element with a sequence. > WARNING: Failure executing file: <bogus.py>
You need to give np.loadtxt a converter so that it converts that first column of strings into datetime objects: import numpy as np import matplotlib.pyplot as plt from matplotlib import dates from datetime import datetime from StringIO import StringIO s=''' 2005-03-04,0.923115796 2005-03-05,0.915828724 2005-03-06,0.442521474 2005-03-07,0.997096213 2005-03-08,0.867752118''' dateparser = lambda s: datetime.strptime(s, '%Y-%m-%d') dt = np.dtype([('date', np.object),('ydata', np.float)]) d = np.loadtxt(StringIO(s), dtype=dt, converters={0:dateparser}, delimiter=',') plt.plot(d['date'], d['ydata']) plt.gca().xaxis.set_major_locator(dates.DayLocator()) plt.gca().xaxis.set_major_formatter(dates.DateFormatter('%Y/%m/%d')) plt.show() Personally, I find it much easier to work with python datetime objects than any other form. You can Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma ------------------------------------------------------------------------------ ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users