這里是以其中5個省份的城市為例進行演示
畫圖前準備
在畫圖之前先導(dǎo)入相關(guān)的庫
1
2
3
|
from pyecharts.charts import map , timeline from pyecharts import options from pyecharts import options as opts |
數(shù)據(jù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# 1. 準數(shù)據(jù) ### 山東省 sd_c = [ '青島市' , '濟南市' , '濰坊市' , '臨沂市' , '煙臺市' , '聊城市' , '濟寧市' , '淄博市' , '菏澤市' , '德州市' , '泰安市' , '濱州市' , '威海市' , '東營市' , '棗莊市' , '日照市' ] sd_d = [ 1278 , 1163 , 834 , 704 , 673 , 632 , 622 , 587 , 586 , 570 , 434 , 401 , 303 , 289 , 216 , 201 ] ### 廣東省 gd_c = [ '廣州市' , '東莞市' , '佛山市' , '汕頭市' , '惠州市' , '中山市' , '揭陽市' , '珠海市' , '江門市' , '潮州市' , '湛江市' , '清遠市' , '汕尾市' , '肇慶市' , '茂名市' , '梅州市' , '韶關(guān)市' , '河源市' , '陽江市' , '云浮市' ] gd_d = [ 1925 , 1235 , 936 , 788 , 548 , 464 , 420 , 356 , 354 , 311 , 274 , 246 , 237 , 220 , 205 , 157 , 127 , 118 , 108 , 82 ] ### 四川省 sc_c = [ '綿陽市' , '南充市' , '廣元市' , '德陽市' , '宜賓市' , '達州市' , '瀘州市' , '廣安市' , '樂山市' , '遂寧市' , '眉山市' , '巴中市' , '涼山彝族自治州' , '內(nèi)江市' , '自貢市' , '資陽市' , '雅安市' , '攀枝花市' , '阿壩藏族羌族自治州' , '甘孜藏族自治州' ] sc_d = [ 944 , 741 , 639 , 555 , 493 , 463 , 359 , 351 , 340 , 337 , 300 , 282 , 239 , 214 , 190 , 174 , 157 , 141 , 123 , 104 ] ### 浙江省 zj_c = [ '杭州市' , '溫州市' , '寧波市' , '金華市' , '嘉興市' , '臺州市' , '紹興市' , '湖州市' , '麗水市' , '衢州市' , '舟山市' ] zj_d = [ 1183 , 792 , 765 , 582 , 438 , 381 , 360 , 288 , 197 , 103 , 66 ] ### 貴州省 gz_c = [ '貴陽市' , '遵義市' , '畢節(jié)市' , '黔南布依族苗族自治州' , '黔西南布依族苗族自治州' , '六盤水市' , '安順市' , '黔東南苗族侗族自治州' , '銅仁市' ] gz_d = [ 1605 , 887 , 454 , 414 , 414 , 381 , 338 , 291 , 196 ] |
這里以5個省份為例,統(tǒng)計5個省份的各市(不完全統(tǒng)計)以及各市的火鍋店數(shù)量
繪圖
這里繪圖主要是用到pyecharts庫的map類和timeline這兩個類
其中map是繪制城市地圖,timeline是繪制輪播圖(整合城市圖)
先看一下官方案例
1
2
3
4
5
|
map1 = ( map (init_opts = opts.initopts(width = "700px" ,height = "300px" ,theme = "blue" )) .add(' ', [(i,j) for i,j in zip(sd_c,sd_d)], ' 山東') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 4000 )) ) |
其init_opts=opts.initopts(width="700px",height="300px",theme="blue")這部分代碼是自定義的,不需要的可以去掉(這里只是修改了默認從參數(shù))
add中:add('', [(i,j) for i,j in zip(sd_c,sd_d)], '山東'),其中sd_c是省份對應(yīng)的市,sd_d是每一個市對應(yīng)的火鍋店數(shù),后面的“山東”是省份,sd_c的市是和和山東對應(yīng)的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 2. 繪制山東省地圖:格式一 map1 = ( map (init_opts = opts.initopts(width = "700px" ,height = "300px" ,theme = "blue" )) .add(' ', [(i,j) for i,j in zip(sd_c,sd_d)], ' 山東') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 4000 )) ) # 3. 繪制廣東省地圖:格式二 map2 = ( map () .add(' ', [(i,j) for i,j in zip(gd_c,gd_d)], ' 廣東') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 400 ,is_piecewise = true)) ) # 4. 繪制四川省地圖:格式二 map3 = ( map () .add(' ', [(i,j) for i,j in zip(sc_c,sc_d)], ' 四川') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 400 ,is_piecewise = true)) ) # 5. 繪制浙江省地圖:格式二 map4 = ( map () .add(' ', [(i,j) for i,j in zip(zj_c,zj_d)], ' 浙江') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 400 ,is_piecewise = true)) ) # 6. 繪制貴州省地圖:格式二 map5 = ( map () .add(' ', [(i,j) for i,j in zip(gz_c,gz_d)], ' 貴州') .set_global_opts(visualmap_opts = opts.visualmapopts(max_ = 400 ,is_piecewise = true)) ) |
繪制輪播圖
將上面的5個城市地圖(map1~map5)整合到一起,并且設(shè)置間隔為3秒進行輪播
1
2
3
4
5
6
7
8
9
10
11
|
# 4. 創(chuàng)建組合類對象 timeline = timeline(init_opts = opts.initopts(width = '720px' , height = '350px' )) # 5. 在組合對象中添加需要組合的圖表對象 timeline.add(chart = map1, time_point = "山東省地圖" ) timeline.add(chart = map2, time_point = "廣東省地圖" ) timeline.add(chart = map3, time_point = "四川省地圖" ) timeline.add(chart = map4, time_point = "浙江省地圖" ) timeline.add(chart = map5, time_point = "貴州省地圖" ) ### 設(shè)置輪播時間 timeline.add_schema(is_auto_play = true, play_interval = 3000 ) |
最后保存成html
1
2
|
# 6. 渲染數(shù)據(jù) timeline.render( "城市地圖輪播圖.html" ) |
最終的效果
小結(jié)
以上就是城市輪播圖的全部講解,代碼量不多,完整源碼:
https://gitee.com/lyc96/carousel-map/tree/master
以上就是用python畫城市輪播地圖的詳細內(nèi)容,更多關(guān)于python 畫輪播地圖的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://mp.weixin.qq.com/s/RL8whxnR-6p58ojaPLri4A