numpy - Python - Plot series from array if above a threshold -
first question , novice python user here.
i'm using matplotlib produce line plots of data series numpy array. arrays 170 x 481, there 169 series of 480 data points each (1st column x-axis values, top row header column names).
in data, of values range between 0. , 1. however, many of these series have data values approximating 0. , i'd plot series have peak value in series above threshold.
a simplified example array 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 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 case i'm interested in plotting a
, b
, not c
. whilst easy manually exclude here, not easy 169 series.
the route i've been trying use for
loop numpy.amax()
plot slices/series above threshold:
for in myarray[:,1:]: if np.amax(myarray[1:,i]) > 0.015: #example threshold plt.plot(myarray[1:,0], myarray[1:,i])
using route unfortunately doesn't work, throws indexerror indices in array floats, not int or bool:
indexerror traceback (most recent call last) <ipython-input-56-d5338b9231af> in <module>() 1 in myarray[:,1:]: ----> 2 if np.amax(myarray[1:,i]) > 0.015: 3 plt.plot(myarray[1:,0], myarray[1:,i]) indexerror: arrays used indices must of integer (or boolean) type
my question how go selecting , plotting series of data possesses element above desired threshold (without modifying data in array)?
edit: following @importanceofbeingernest 's answer, requires whole array floats (it turns out inserting header strings array @ earlier time had inadvertently turned float data strings). in case typeerror, keep in mind if encounters that.
you want loop on indizes of array along columns.
for in range(1, myarray.shape[1]): if np.amax(myarray[1:,i]) > 0.015: plt.plot(myarray[1:,0], myarray[1:,i])
Comments
Post a Comment