Plotting syntax

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Feb 7 11:26:50 CET 2013


On 7 February 2013 09:37, Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:
> ----- Original Message -----
>
>> 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])
>>
[SNIP]
>
> x = x[0:4]+ x[5:8]
> y = y[0:4]+ y[5:8]
>
> skips the element at index 4, meaning the fifth columns.
> Alternatively,
>
> x = x[:4]+ x[5:]
> y = y[:4]+ y[5:]
>
> skips the 5th element without regard for the length of the list.
> http://docs.python.org/release/2.3.5/whatsnew/section-slices.html

I'm guessing from the multi-dimensional slice syntax that d is a numpy
array in which case the + operator attempts to sum the two arrays
element-wise (resulting in an error if they are not the same shape):

>>> from numpy import array
>>> a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
>>> a
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])
>>> a[:,0]
array([ 1,  4,  7, 10])
>>> a[:,1]
array([ 2,  5,  8, 11])
>>> a[:,0] + a[:,1]
array([ 3,  9, 15, 21])
>>> a[:,0] + a[:, 1:]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (4) (4,2)


To do this with a numpy array use fancy indexing:

>>> a[:, [0, 2]]
array([[ 1,  3],
       [ 4,  6],
       [ 7,  9],
       [10, 12]])

So a solution could be:

included = [0, 1, 2, 3, 5, 6, 7]
oplot (t,d[:, included])


Oscar



More information about the Python-list mailing list