【发布时间】:2019-07-31 15:27:11
【问题描述】:
我正在尝试从天气网站的 api 构建图表。我希望用户输入我想知道当前温度的 3 个城市,然后从 api 和用户输入 {name of the city - user input: the currently temp from the api} 制作字典,然后将字典制作成图表 {name of the city: the currently temp from the api}
我使用的api是drksky api
我主要使用这个:
import matplotlib.pyplot as plt for the graph
import forecastio
import matplotlib.pyplot as plt
def read_key_from_file():
hFile = open(file_name,'r')
for line in hFile:
Mykey = line
hFile.close()
Mykey = line
return Mykey
def get_geo_location(location):
location = Nominatim().geocode(location)
lat = location.latitude
lon = location.longitude
return location, lat, lon
def comperison_graph(Mykey):
if daily_temp_dict == {}:
a = input("Enter first city name in lower case letter: ")
daily_temp_dict[a] = ""
b = input("Enter second city name in lower case letter: ")
daily_temp_dict[b] = ""
c = input("Enter third city name in lower case letter: ")
daily_temp_dict[c] = ""
for city in daily_temp_dict.keys():
location, lat, lon = get_geo_location(city)
forecast = forecastio.load_forecast(Mykey, lat, lon)
daily_temp_dict[city] = forecast.currently().temperature
data = daily_temp_dict
names = list(data.keys())
print(names)
values = list(data.values())
print(values)
plt.bar(0,values[0],tick_label=names[0])
plt.bar(1,values[1],tick_label=names[1])
plt.bar(2,values[2],tick_label=names[2])
plt.xticks(range(0,3),names)
plt.savefig('fruit.png')
plt.show()
问题是我得到的结果是这样的:
我只需要三个城市的最后一个
很遗憾,我做不到
有人可以帮帮我吗?
【问题讨论】:
-
很遗憾我做不到 - 为什么?有错误吗?输出不是你期望的那样吗?
-
您好,请对您的问题进行一些编辑以增加可读性,您将获得更多关注和更多机会获得答案。
-
它应该是字典中的图表(x 图:城市名称,y 图:来自 api 的当前温度)
-
这条线是什么?
end if -
我修复了代码请有人可以帮助我?
标签: python python-3.x dictionary