我正在从http://data.rcc-acis.org/StnData检索数据。效果很好,我可以使用matplotlib可视化数据。我正在努力为值绘制一条4多项式线。我的代码在下面列出。我正在使用Python 3.7。任何帮助是极大的赞赏。
import urllib
import datetime
import urllib.request
import ast
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import scipy.interpolate as ntrpl
import numpy as np
import matplotlib as mpl
# create a static day of the month
#dayofmonth = ('01-01')
dayofmonth = ('11-26')
# testing out a range of years
y = datetime.datetime.today().year
years = list(range(y-1,y-50, -1))
i = 0
dateList=[]
minTList=[]
maxTList=[]
for year in years:
sdate = (str(year) + '-' + dayofmonth)
#print(sdate)
url = "http://data.rcc-acis.org/StnData"
values = {
"sid": "KGGW",
"date": sdate,
"elems": "maxt,mint",
"meta": "name",
"output": "json"
}
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
results = response.read()
results = results.decode()
results = ast.literal_eval(results)
if i < 1:
n_label = results['meta']['name']
i = 2
for x in results["data"]:
date,maxT,minT = x
#setting the string of date to datetime
date = date[0:4]
date_obj = datetime.datetime.strptime(date,'%Y')
dateList.append(date_obj)
minTList.append(minT)
maxTList.append(maxT)
minT_int = list(map(int,minTList))
maxT_int = list(map(int,maxTList))
n_label = n_label + " " + dayofmonth
#p1 = np.polyfit(newDates,h_temps,3)
def plot_graph(dates,h_temps,l_temps,label):
#converting the dates into a readable format for pyplot
newDates = mdates.date2num(dates)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.title(label)
plt.gcf().autofmt_xdate()
plt.plot(newDates,l_temps)
plt.plot(newDates,h_temps)
#p1 = np.polyfit(x,y,3)
p1 = np.polyfit(newDates,h_temps,3)
print(p1)
plt.plot(newDates,p1)
plt.show()
#print
plot_graph(dateList,maxT_int,minT_int,n_label)