On Sun, Jan 9, 2011 at 11:34 AM, Adam Mercer <ramer...@gmail.com> wrote:

> Hi
>
> In one of my codes I need to plot several time series from different
> files, the files are of the form
>
> 20100118 10
> 20100119 12
> 20100120 14
> 20100121 16
> 20100126 18
> 20100221 25
> 20100222 25
> 20100227 26
> 20100228 30
>
> I use something like the following to plot these:
>
> morning = numpy.loadtxt(morning_file, converters={0: dates.datestr2num})
> morning_plot = date_axes.plot_date(morning[:,0], morning[:,1], 'bo-', ms=4)
>
> However sometimes these files only contain a single line and my script
> fails with an error:
>
> Traceback (most recent call last):
>  File "./plot.py", line 119, in <module>
>    morning_plot = date_axes.plot_date(morning[:,0], morning[:,1], 'ro-',
> ms=4)
> IndexError: invalid index
>
> Is there a way that I can plot these files regardless of whether they
> contain multiple or single lines?
>
> Cheers
>
> Adam
>
>
Adam,

You have run into a peculiar numpy bug that I have reported several months
ago.  Essentially, np.loadtxt() does a squeeze() on the data right before
returning it.  Therefore, if there is only one line, the array returned is a
1-d array rather than your expected 2d array.

You can mitigate this by using np.atleast_2d() on the returned array.  This
will guarantee that your 'morning' array will always be 2d.

I hope that helps!
Ben Root
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to