ploting single and multiple points

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

···

On Sun, Jan 9, 2011 at 11:34 AM, Adam Mercer <ramercer@…287…> 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

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

It does! Thanks!

Cheers

Adam

···

On Sun, Jan 9, 2011 at 13:04, Benjamin Root <ben.root@...1304...> wrote:

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!