在爬取到中国天气的最低气温数据之后,想进行可视化,按照资料上的教学,源代码如下:
导入的库:
from pyecharts import Bar
可视化代码如下:
......
ALL_DATA.sort(key=lambda data: data['min_temp'])
data = ALL_DATA[0:10]
cities = list(map(lambda x: x['city'], data))
min_temps = list(map(lambda x: x['min_temp'], data))
chart = Bar("中国天气最低气温排行")
chart.add("", cities, min_temps)
chart.render('temperature.html')
......
结果一直报错,“AttributeError: 'str' object has no attribute 'get'”,检查之后,发现应该是bar的对象导入问题,博主python小白,也不知道咋回事,上网百度了很多资料,结果代码千篇一律没一个不一样的,更搞笑的是看到一篇2019年7月的博文说什么用这个代码爬取中国天气balabala,说的煞有其事,不知道博主检查过没?就照着往上抄。
解决办法(看githup上的资料,网址:https://github.com/pyecharts/pyecharts)
导入的库如下:
from pyecharts.charts import Bar
可视化代码修改如下:
ALL_DATA.sort(key=lambda data:data['temp'])
data = ALL_DATA[0:20]
cities = list(map(lambda x:x['city'], data))
temps = list(map(lambda x:x['temp'], data))
bar = (
Bar()
.add_xaxis(cities)
.add_yaxis("张张",temps)
.set_global_opts(title_opts=opts.TitleOpts(title="中国天气最低气温排行"))
)
bar.render()
最后成果如下:
注:Python 3.7.3 (default, Jun 18 2019, 00:39:11)
pyecharts: 1.3.1
工具 :pycharm
苹果系统:10.13.4