Home > Mobile >  How can I use fill_between if the points I have are array with single value
How can I use fill_between if the points I have are array with single value

Time:01-01

I have a matplotlib script.

In it in the end I have

x_val=[x[0] for x in lista]
y_val=[x[1] for x in lista]
z_val=[x[2] for x in lista]

ax.plot(x_val,y_val,'.-')
ax.plot(x_val,z_val,'.-')

This script plots well eventhough the values in y_val and z_val are not strictly numbers

Debugging I have

(Pdb) x_val
[69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153]
(Pdb) y_val
[array(1.74204588), array(1.74162786), array(1.74060187), array(1.73956786), array(-1.89492498), array(-1.89225716), array(-1.89842406), array(-1.89143466), array(-1.89171231), array(-1.88730752), array(-1.89144205), nan, array(1.71829279), array(-1.88108125), array(-1.87515878), array(-1.87912412), array(-1.87015615), array(-1.87152107), array(-1.86639765), array(-1.87383146), array(-1.86896753), array(-1.87339903), array(-1.8748417), array(-1.88515482), array(-1.88263666), array(-1.88571425), nan, nan, array(1.72480822), array(1.73666841), array(-1.88835078), array(-1.88489648), array(-1.89135095), array(-1.88647712), array(-1.88697799), array(-1.88330942), array(-1.88929744), array(-1.88320532), array(-1.88466698), array(-1.87994435), array(-1.88546968), array(-1.88014776), array(-1.87803843), array(-1.87505217), array(-1.8797416), array(-1.87223076), array(-1.87333355), array(-1.86838693), array(-1.87577428), array(-1.86875561), array(-1.86872998), array(-1.86385078), array(-1.87095955), array(-1.86509266), array(-1.86601095), array(-1.86223456), array(-1.87151403), array(-1.86695325), array(-1.86540432), array(-1.86244142), array(-1.87018407), array(-1.86767604), array(-1.8699986), array(-1.87008087), array(-1.88049869), array(1.70057683), array(1.74942263), array(-1.86556665), array(-1.88470081), array(-1.90776552), array(-1.9103818), array(-1.91022515), array(-1.89490587), array(-1.89507617), array(-1.8875979), array(-1.89318633), array(-1.8942595), array(-1.902641), array(-1.89313615), array(-1.87870174), array(-1.86319541), array(-1.85999368), array(-1.85943922), array(-1.88398592), array(1.73030903)]

z_val similarly

This does not represent a problem However I want to do

ax.fill_between(x_val,0,1,where=(y_val*z_val) >0, 
                color='green',alpha=0.5  )

It is a first attempt that I will probably modify (in this example for instance I don't understand yet what transform=ax.get_xaxis_transform() does) but the problem is that now I got an error

  File "plotgt_vs_time.py", line 160, in plot
    ax.fill_between(x_val,0,1,where=(y_val*z_val) >0, 
TypeError: can't multiply sequence by non-int of type 'list'

I suppose it is because it is an array. How can I modify my code so as to be able to use fill_between?

I tried modifying it to

x_val=[x[0] for x in lista]
y_val=[x[1][0] for x in lista]
z_val=[x[2][0] for x in lista]

but this throws an error

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

Then I modified it to

x_val=[x[0] for x in lista]
y_val=[float(x[1]) for x in lista]
z_val=[float(x[2]) for x in lista]

And now I only get floats, so I eliminated the 0-D arrays but still got the error

TypeError: can't multiply sequence by non-int of type 'list'

How can I use fill_beetween?

CodePudding user response:

In the end I solve it transforming the lists into numpy arrays

x_val=[x[0] for x in lista]
y_val=[float(x[1]) for x in lista]
z_val=[float(x[2]) for x in lista]

ax.fill_between(x_val,y_val,z_val,where=(np.array(y_val)*np.array(z_val)) >0, 
               color='red',alpha=0.5  )
  • Related