目标:对多年的降雪进行可视化
单色彩 | 多色彩 |
---|---|
速度被加快了(数据有些大,时间间隔过小会不显示)
1 前期准备
依赖安装
pip install pyecharts -U
# 如果需要安装 0.5.11 版本的开发者,可以使用
# pip install pyecharts==0.5.11
地图安装:中国区
(注意南海地图完整度,github的地图因此被下架)
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
pip install echarts-china-counties-pypkg
pip install echarts-china-misc-pypkg
pip install echarts-united-kingdom-pypkg
数据预处理(坑#1)
目标:打点
源码:
def add_coordinate(
self, name: str, longitude: types.Numeric, latitude: types.Numeric
):
self._coordinates.update({name: [longitude, latitude]})
return self
def add_coordinate_json(self, json_file: str):
with open(json_file, "r", encoding="utf-8") as f:
json_reader = json.load(f)
for k, v in json_reader.items():
self.add_coordinate(k, v[0], v[1])
return self
可以看到
使用 add_coordinate 方法输入(str, num, num)
使用 add_coordinate_json 方法输入(path)
path为json文件路径,而不是字符串。
当时把json的String放进去一直报错的原因在此。
2 打出多个点
#官方例子
from example.commons import Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
def geo_base() -> Geo:
c = (
Geo()
.add_schema(maptype="china")
.add("geo", [list(z) for z in zip(Faker.provinces, Faker.values())])
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(),
title_opts=opts.TitleOpts(title="Geo-基本示例"),
)
)
return c
geo_base().render_notebook() #在jupyter
geo_base().render("china_snow.html")# 在本地
#二选一
导入即报错,只能从GitHub的代码里寻找导入方式,转为json以后无法显示,所以换为循环输入
选择 add_coordinate(x,x,x)
我的解决方法:
datalist=[['50246.0', 124.43000000000005, 52.2![微信图片_20191001as221723.png](https://upload-images.jianshu.io/upload_images/17009238-b7a4cbd902309fb0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1, 14.166666666666666],
['50247.0', 123.33999999999999, 52.01999999999999, 33.666666666666664],
['50349.0', 124.24, 51.399999999999984, 23.833333333333332],
['50353.0', 126.38000000000004, 51.44, 16.416666666666668],
['50425.0', 120.10999999999997, 50.149999999999984, 13.5],
['50434.0', 121.41000000000001, 50.29, 22.5],
['50442.0', 124.06999999999995, 50.24, 24.75],
['50514.0', 117.19000000000004, 49.350000000000016, 12.916666666666666],
['50527.0', 119.42000000000002, 49.149999999999984, 13.25],
['50548.0', 123.43000000000005, 49.12, 19.166666666666668],
['50557.0', 125.14000000000003, 49.097500000000004, 17.333333333333332],
['50564.0', 127.21166666666669, 49.261666666666656, 20.25],
['50603.0', 116.49, 48.40999999999999, 6.083333333333333],
['50618.0', 118.16000000000001, 48.13, 21.0],
['50632.0', 121.54999999999997, 48.46, 16.333333333333332],
['50639.0', 122.44000000000004, 48.0, 14.666666666666666],
['50656.0', 126.29999999999997, 48.149999999999984, 26.083333333333332]]
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Map, Page
from pyecharts.faker import Collector, Faker
from pyecharts.charts import Geo
import pandas as pd
from pyecharts.faker import Collector, Faker
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = Geo()
for i in datalist:
(c
.add_schema(maptype="china")
# 加入自定义的点,格式为
.add_coordinate(i[0],i[1],i[2])
# 为自定义的点添加属性
#.add(i[0])
.add("snow", [(i[0],i[3] )],symbol_size=np.log((i[3]/10)+1)*10)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="降雪量分布"))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(range_color=Faker.visual_color),
title_opts=opts.TitleOpts(title="Geo-降雪量分布"),
)
)
# 在 Jupyter Notebook 中渲染图表
c.render_notebook()
#在jupyter
#c.render("china_snow.html")# 在本地
#二选一
3.时间轴
数据格式:
{1961: date1,1962:date2..........}
year_data_list={1961: [['50246.0', 124.43000000000005, 52.18999999999999, 44.63636363636363],
['50353.0', 126.39000000000003, 51.429999999999986, 45.0],
['50425.0', 120.05999999999996, 50.279999999999994, 31.5],
['50434.0', 121.27999999999999, 50.300000000000004, 48.083333333333336],
['50514.0', 117.26, 49.37, 18.166666666666668],
['50527.0', 119.45000000000003, 49.13, 20.0],
['50548.0', 123.52999999999999, 49.050000000000004, 27.916666666666668]],
1962: [['50246.0', 124.43000000000005, 52.18999999999999, 44.63636363636363],
['50353.0', 126.39000000000003, 55.429999999999986, 45.0],
['50425.0', 120.05999999999996, 55.279999999999994, 31.5],
['50434.0', 121.27999999999999, 55.300000000000004, 48.083333333333336],
['50514.0', 117.26, 49.37, 18.166666666666668],
['50527.0', 119.45000000000003, 45.13, 20.0],
['50548.0', 123.52999999999999, 49.050000000000004, 27.916666666666668]],
1963: [['50246.0', 124.43000000000005, 52.18999999999999, 44.63636363636363],
['50353.0', 126.39000000000003, 51.429999999999986, 45.0],
['50425.0', 120.05999999999996, 50.279999999999994, 31.5],
['50434.0', 121.27999999999999, 43.300000000000004, 48.083333333333336],
['50514.0', 117.26, 49.37, 18.166666666666668],
['50527.0', 119.45000000000003, 49.13, 20.0],
['50548.0', 123.52999999999999, 49.050000000000004, 27.916666666666668]],
1964: [['50246.0', 124.43000000000005, 52.18999999999999, 44.63636363636363],
['50353.0', 126.39000000000003, 51.429999999999986, 45.0],
['50425.0', 120.05999999999996, 56.279999999999994, 31.5],
['50434.0', 121.27999999999999, 50.300000000000004, 48.083333333333336],
['50514.0', 117.26, 49.37, 18.166666666666668],
['50527.0', 119.45000000000003, 49.13, 20.0],
['50548.0', 123.52999999999999, 49.050000000000004, 27.916666666666668]],
1965: [['50246.0', 124.43000000000005, 52.18999999999999, 44.63636363636363],
['50353.0', 126.39000000000003, 51.429999999999986, 45.0],
['50425.0', 120.05999999999996, 47.279999999999994, 31.5],
['50434.0', 121.27999999999999, 50.300000000000004, 48.083333333333336],
['50514.0', 117.26, 49.37, 18.166666666666668],
['50527.0', 119.45000000000003, 49.13, 20.0],
['50548.0', 123.52999999999999, 49.050000000000004, 27.916666666666668]],
}
def get_year_chart(year: int,data=year_data_list):
cmm = year_data_list[year]
c = Geo()
for i in cmm:
(c
.add_schema(maptype="china")
# 加入自定义的点,格式为
.add_coordinate(i[0],i[1],i[2])
# 为自定义的点添加属性
#.add(i[0])
.add("snow", [(i[0],i[3] )],symbol_size=np.log((i[3]/10)+1)*10)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="降雪量分布"))
.set_global_opts(
#visualmap_opts=opts.VisualMapOpts(range_color=Faker.visual_color), # 这里是变彩色的地方使用预设颜色
title_opts=opts.TitleOpts(title="Geo-降雪量分布"),
)
)
grid_chart = (
Grid()
.add(c, grid_opts=opts.GridOpts())
)
return grid_chart
##时间轴函数
time_list = [1961,1962,1963,1964,1965]
timeline = Timeline(
init_opts=opts.InitOpts(theme=ThemeType.LIGHT)
)
for y in time_list:
g = get_year_chart(y)
timeline.add(g, time_point=str(y))
timeline.options.get("baseOption").get("timeline").update(
{"playInterval": 500}) #自动播放的切换时间间隔
timeline.render_notebook()
#timeline.render("china_snow.html")
总结
两个版本差异巨大,新版本文档没有对方法的解释,官方文档需要翻墙,而且导包处就报错,坑多。
阅读源码和文档对解决bug至关重要(奈何搜索无功)
缺点:暂时还没找到对动画渐变的修改选项,要是有渐变效果就好了(可能是我没发现)
终极效果太美丽,妥妥的仪表盘: