下面有一些代码,我基本上是想在一个测深仪上绘制风钩
import urllib
import matplotlib.pyplot as plt
%matplotlib inline
def read_sounding(url):
pressure=[]
altitude=[]
temp =[]
lines = urllib.request.urlopen(url).readlines()
for line in lines[10:76]:
entries = line.decode("utf-8").split(" ")
columns=[]
for entry in entries:
if len(entry) > 1: columns.append(entry)
if len(columns) > 2: # otherwise data not available
pressure.append(float(columns[0]))
altitude.append(float(columns[1]))
temp.append(float(columns[2]))
return(pressure,altitude,temp)
def read_wind(url):
wind = []
lines = urllib.request.urlopen(url).readlines()
for line in lines[10:76]:
entries = line.decode("utf-8").split(" ")
columns=[]
for entry in entries:
if len(entry) > 1: columns.append(entry)
if len(columns) > 2: # otherwise data not available
wind.append(float(columns[7]))
return(wind)
def location(url):
lines = urllib.request.urlopen(url).readlines()
lon=lines[81] # longitude
lat=lines[80] # latitude
lon=float(lon.decode("utf-8").split(":")[1])
lat=float(lat.decode("utf-8").split(":")[1])
return(lat,lon)
if __name__ == '__main__':
url = 'http://weather.uwyo.edu/cgi-bin/sounding?region=naconf&TYPE=TEXT%3ALIST&YEAR=2005&MONTH=07&FROM=0812&TO=0812&STNM=72469'
p,h,t=read_sounding(url)
lat,lon=location(url)
W= read_wind(url)
plt.plot(t,h,'o--')
#print(W)
plt.barbs(W)
plt.xlabel("temperature [C]")
plt.ylabel("altitude [m]")
plt.title('lat='+str(lat)+' lon='+str(lon))
我得到的错误是在plt.barb行中,它的错误提示为“从空列表弹出”,即使我打印返回的值时也有一个带有数字的列表。我可以绘制一个测温曲线,但需要在其上添加风钩。 main函数中的链接在列中包含所有数据。 确切的错误是“索引错误:从空列表中弹出”