Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib fill between method and datetime

I am trying to use the matplotlib fill_between function, yet I got the following error "TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"

I am using the pandas datetime in my x-axes, and think that this raise such problem. I am not yet able to find a solution.

Here is the code

#%%
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Open file
f = open('rmm.74toRealtime.txt', 'r')

# Read and ignore header lines
header1 = f.readline()
header2 = f.readline()

n=15461-2
year=np.zeros(n)
month=np.zeros(n)
day=np.zeros(n)
rmm1=np.zeros(n)
rmm2=np.zeros(n)
phase=np.zeros(n)
ampl=np.zeros(n)
a=np.zeros(n)
b=np.zeros(n)

i=0
# Loop over lines and extract variables of interest
for line in f:
    line = line.strip()
    columns = line.split()
    year[i] = columns[0]
    month[i]=columns[1]
    day[i]=columns[2]
    rmm1[i]=columns[3]
    rmm2[i]=columns[4]
    phase[i]=columns[5]
    ampl[i]=columns[6]
    print([float(year[i]),float(month[i]),float(day[i]),float(rmm1[i]),float(rmm2[i]),float(phase[i]),float(ampl[i])])
    a[i]=str(int(year[i]))+str(int(month[i])).zfill(2) +str(int(day[i])).zfill(2) ;
    i=i+1
f.close()

w=pd.to_datetime(pd.Series(a),format='%Y%m%d')
yt=np.array(np.where((month == 1) & (day ==1)))
a=np.reshape(yt,len(yt[0,:]))

#%%
plt.close('all')
for i in a[:1]:   
    plt.figure()
    plt.plot(w[i:i+365],ampl[i:i+365])
    plt.fill_between(w[i:i+365],ampl[i:i+365],1)
    plt.title('RMM '+str(int(year[i])))
    plt.grid()
    plt.show()

and here is a link for the data file ascii_file

Thanks

like image 419
Kernel Avatar asked Sep 12 '26 06:09

Kernel


1 Answers

A solution to this problem can be found in this answer. Thus, try

plt.fill_between(w.values[i:i+365],ampl[i:i+365],1)
like image 52
ImportanceOfBeingErnest Avatar answered Sep 14 '26 19:09

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!