Re: Plotting syntax

Discussion in 'Python' started by Steven D'Aprano, Feb 7, 2013.

1. Steven D'ApranoGuest

On Wed, 06 Feb 2013 10:37:07 -0500, Vladimir Ivkovic wrote:

> Hi Python experts,
>
> I am working with an array of data and am trying to plot several columns
> of data which are not continuous; i.e. I would like to plot columns 1:4
> and 6:8, without plotting column 5. The syntax I am currently using is:
>
> oplot (t,d[:,0:4])
>
> The question is: How do I specify within the above command, for columns
> 6:8 to be plotted?

You don't give us enough information to be sure:

- you don't tell us what library oplot comes from;

- you don't tell us what t is;

- you don't tell us what d is;

- and d[:,0:4] looks like it could be a SyntaxError or a typo.

But assuming that what you tell us actually is correct, then obviously if
oplot (t,d[:,0:4]) plots columns 1-4, then oplot (t,d[:,5:8]) will surely
plot columns 6-8.

Python counts starting from 0, not 1, and uses half-open intervals: when
giving a range of indexes, like 0:4, Python interprets that as follows:

item at index 0 (the first item)
item at index 1 (the second item)
item at index 2 (the third item)
item at index 3 (the fourth item)

with index 4 being excluded.

So 5:8 will include the sixth through eighth items.

This notation is called "slicing", and d[0:4] is called "a slice".

The idea is that if you number the items starting from zero, and imagine
the boundaries between items (shown as | vertical lines):

|0|1|2|3|4|5|6|7|8|9|10|11 ...

then always slice on the boundary to the left of the given number:

slice [0:4] => |0|1|2|3|

slice [5:8] => |5|6|7|

The only tricky part is remembering to count from zero instead of one.

--
Steven

Steven D'Aprano, Feb 7, 2013