Dave
Dave

Reputation: 21

Python - Plot series from array if above a threshold

First question and fairly novice python user here.

I'm using matplotlib to produce line plots of data series from a numpy array. The arrays are 170 x 481, where there are 169 series of 480 data points each (1st column are my x-axis values, top row is a header with column names).

In my data, all of the values range between 0. and 1. However, many of these series have data values approximating to 0. and so I'd like to only plot series that have a peak value in the series above a certain threshold.

A simplified example array would be the following:

myarray = [['thickness' 'a' 'b' 'c']
 ['0.25' '0.5' '0.2' '0.001']
 ['0.5' '0.4' '0.3' '0.002']
 ['0.75' '0.3' '0.2' '0.001']]

Manually plotting with matplotlib:

plt.plot(myarray[1:,0], myarray[1:,1], label='a')
plt.plot(myarray[1:,0], myarray[1:,2], label='b')
plt.plot(myarray[1:,0], myarray[1:,3], label='c')
plt.xlabel('Thickness')
plt.ylabel('Intensity')
plt.legend(loc='upper right')

Result here.

In this case I'm interested in plotting a and b, but not c. Whilst it is easy to manually exclude here, this is not so easy with 169 series.

The route I've been trying is to use a for loop with numpy.amax() to only plot slices/series above a threshold:

for i in myarray[:,1:]:
    if np.amax(myarray[1:,i]) > 0.015: #example threshold
        plt.plot(myarray[1:,0], myarray[1:,i])

Using this route unfortunately doesn't work, as it throws up an IndexError as the indices in the array are floats, not int or bool:

IndexError                                Traceback (most recent call last)
<ipython-input-56-d5338b9231af> in <module>()
      1 for i in myarray[:,1:]:
----> 2     if np.amax(myarray[1:,i]) > 0.015:
      3         plt.plot(myarray[1:,0], myarray[1:,i])

IndexError: arrays used as indices must be of integer (or boolean) type

My question specifically is how would you go about selecting and plotting a series of data that possesses an element above a desired threshold (without modifying the data in the array)?

EDIT: Following @ImportanceOfBeingErnest 's answer, it requires the whole array to be floats (it turns out by inserting the header strings into my array at an earlier time I had inadvertently turned the float data to strings). In this case you get a TypeError, so keep this in mind if anyone encounters that.

Upvotes: 0

Views: 2804

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339560

You want to loop over the indizes of your array along the columns.

for i in range(1, myarray.shape[1]):
    if np.amax(myarray[1:,i]) > 0.015: 
        plt.plot(myarray[1:,0], myarray[1:,i])

Upvotes: 0

Related Questions