一文帶你掌握Pyecharts地理數(shù)據(jù)可視化的方法
本文主要介紹了Pyecharts地理數(shù)據(jù)可視化,分享給大家,具體如下:
一、Pyecharts簡(jiǎn)介和安裝
1. 簡(jiǎn)介
Echarts 是一個(gè)由百度開(kāi)源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),得到了眾多開(kāi)發(fā)者的認(rèn)可。而 Python 是一門(mén)富有表達(dá)力的語(yǔ)言,很適合用于數(shù)據(jù)處理。當(dāng)數(shù)據(jù)分析遇上數(shù)據(jù)可視化時(shí),pyecharts 誕生了。
- 簡(jiǎn)潔的 API 設(shè)計(jì),使用如絲滑般流暢,支持鏈?zhǔn)秸{(diào)用
- 囊括了 30+ 種常見(jiàn)圖表,應(yīng)有盡有
- 支持主流 Notebook 環(huán)境,Jupyter Notebook 和 JupyterLab
- 可輕松集成至 Flask,Sanic,Django 等主流 Web 框架
- 高度靈活的配置項(xiàng),可輕松搭配出精美的圖表
- 詳細(xì)的文檔和示例,幫助開(kāi)發(fā)者更快的上手項(xiàng)目
- 多達(dá) 400+ 地圖文件,并且支持原生百度地圖,為地理數(shù)據(jù)可視化提供強(qiáng)有力的支持
pyecharts版本v0.5.x 和 v1 間不兼容,v1 是一個(gè)全新的版本,語(yǔ)法也有很大不同。
2. 安裝
安裝pyecharts
pip install pyecharts -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
import pyecharts print(pyecharts.__version__) # 查看當(dāng)前pyecharts版本
安裝相關(guān)的地圖擴(kuò)展包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-countries-pypkg # 全球國(guó)家地圖 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-provinces-pypkg # 中國(guó)省級(jí)地圖 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-cities-pypkg # 中國(guó)市級(jí)地圖 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple echarts-china-counties-pypkg # 中國(guó)縣區(qū)級(jí)地圖
二、地圖可視化
1. 世界地圖
利用 Starbucks.csv 中的數(shù)據(jù),首先計(jì)算每個(gè)國(guó)家(Country)對(duì)應(yīng)的門(mén)店數(shù)量,然后使用世界地圖可視化展示星巴克門(mén)面店在全球的數(shù)量分布。
# -*- coding: UTF-8 -*- """ @File :demo1.py @Author :葉庭云 @CSDN :https://yetingyun.blog.csdn.net/ """ import pandas as pd from pyecharts.charts import Map from pyecharts import options as opts from pyecharts.globals import ThemeType, CurrentConfig CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # pandas讀取csv文件里的數(shù)據(jù) df = pd.read_csv("Starbucks.csv")['Country'] # 統(tǒng)計(jì)各個(gè)地區(qū)星巴克門(mén)店數(shù)量 data = df.value_counts() datas = [(i, int(j)) for i, j in zip(data.index, data.values)] # 實(shí)例化一個(gè)Map對(duì)象 map_ = Map(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION)) # 世界地圖 map_.add("門(mén)店數(shù)量", data_pair=datas, maptype="world") map_.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) # 不顯示label map_.set_global_opts( title_opts=opts.TitleOpts(title="星巴克門(mén)店數(shù)量在全球分布", pos_left='40%', pos_top='10'), # 調(diào)整title位置 legend_opts=opts.LegendOpts(is_show=False), visualmap_opts=opts.VisualMapOpts(max_=13608, min_=1, is_piecewise=True, pieces=[{"max": 9, "min": 1, "label": "1-9", "color": "#00FFFF"}, # 分段 添加圖例注釋和顏色 {"max": 99, "min": 10, "label": "10-99", "color": "#A52A2A"}, {"max": 499, "min": 100, "label": "100-499", "color": "#0000FF "}, {"max": 999, "min": 500, "label": "500-999", "color": "#FF00FF"}, {"max": 2000, "min": 1000, "label": "1000-2000", "color": "#228B22"}, {"max": 3000, "min": 2000, "label": "2000-3000", "color": "#FF0000"}, {"max": 20000, "min": 10000, "label": ">=10000", "color": "#FFD700"} ]) ) # 渲染在網(wǎng)頁(yè)上 map_.render('星巴克門(mén)店在全球的分布.html')
運(yùn)行效果如下:
2. 國(guó)家地圖
漣漪散點(diǎn)圖
利用 china.csv 中的數(shù)據(jù),首先計(jì)算每個(gè)城市(City)對(duì)應(yīng)的門(mén)店數(shù)量,然后使用 pyecharts 包內(nèi) Geo 模塊繪制星巴克門(mén)面店在中國(guó)各城市的數(shù)量分布的漣漪散點(diǎn)地圖。
import pandas as pd from pyecharts.globals import ThemeType, CurrentConfig, GeoType from pyecharts import options as opts from pyecharts.charts import Geo CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # pandas讀取csv文件數(shù)據(jù) df = pd.read_csv("china.csv")['City'] data = df.value_counts() datas = [(i, int(j)) for i, j in zip(data.index, data.values)] print(datas) geo = Geo(init_opts=opts.InitOpts(width='1000px', height='600px', theme=ThemeType.DARK)) geo.add_schema(maptype='china', label_opts=opts.LabelOpts(is_show=True)) # 顯示label 省名 geo.add('門(mén)店數(shù)量', data_pair=datas, type_=GeoType.EFFECT_SCATTER, symbol_size=8) geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) geo.set_global_opts(title_opts=opts.TitleOpts(title='星巴克門(mén)店在中國(guó)的分布'), visualmap_opts=opts.VisualMapOpts(max_=550, is_piecewise=True, pieces=[{"max": 50, "min": 0, "label": "0-50", "color": "#708090"}, # 分段 添加圖例注釋 和顏色 {"max": 100, "min": 51, "label": "51-100", "color": "#00FFFF"}, {"max": 200, "min": 101, "label": "101-200", "color": "#00008B"}, {"max": 300, "min": 201, "label": "201-300", "color": "#8B008B"}, {"max": 600, "min": 500, "label": "500-600", "color": "#FF0000"}, ]) ) geo.render("星巴克門(mén)店在中國(guó)的分布.html")
運(yùn)行效果如下:
動(dòng)態(tài)軌跡圖
# -*- coding: UTF-8 -*- """ @File :demo3.py @Author :葉庭云 @CSDN :https://yetingyun.blog.csdn.net/ """ from pyecharts import options as opts from pyecharts.charts import Geo from pyecharts.globals import ChartType, SymbolType, CurrentConfig, ThemeType CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # 鏈?zhǔn)秸{(diào)用 c = ( Geo() .add_schema( maptype="china", itemstyle_opts=opts.ItemStyleOpts(color="#323c48", border_color="#111"), label_opts=opts.LabelOpts(is_show=True) ) .add( "", [("廣州", 55), ("北京", 66), ("杭州", 77), ("重慶", 88), ('成都', 100), ('???, 80)], type_=ChartType.EFFECT_SCATTER, color="white", ) .add( "", [("廣州", "上海"), ("廣州", "北京"), ("廣州", "杭州"), ("廣州", "重慶"), ('成都', '???), ('海口', '北京'), ('???, '重慶'), ('重慶', '上海') ], type_=ChartType.LINES, effect_opts=opts.EffectOpts( symbol=SymbolType.ARROW, symbol_size=6, color="blue" # 軌跡線(xiàn)藍(lán)色 ), linestyle_opts=opts.LineStyleOpts(curve=0.2), # 軌跡線(xiàn)彎曲度 ) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts=opts.TitleOpts(title="動(dòng)態(tài)軌跡圖")) .render("geo_lines_background.html") )
運(yùn)行效果如下:
3. 省市地圖
熱力圖
# -*- coding: UTF-8 -*- """ @File :demo4.py @Author :葉庭云 @CSDN :https://yetingyun.blog.csdn.net/ """ from pyecharts import options as opts from pyecharts.charts import Geo from pyecharts.faker import Faker from pyecharts.globals import GeoType, CurrentConfig CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' c = ( Geo() .add_schema(maptype="廣東", label_opts=opts.LabelOpts(is_show=True)) .add( "熱力圖", [list(z) for z in zip(Faker.guangdong_city, Faker.values())], type_=GeoType.HEATMAP, ) .set_series_opts(label_opts=opts.LabelOpts(is_show=True)) .set_global_opts( visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title="Geo-廣東地圖") ) .render("geo_guangdong.html") )
運(yùn)行效果如下:
地圖上批量添加經(jīng)緯度數(shù)據(jù)
數(shù)據(jù)來(lái)源于美團(tuán)網(wǎng)成都地區(qū)酒店信息,利用其中酒店的經(jīng)緯度數(shù)據(jù),批量添加在地圖上可視化。
# -*- coding: UTF-8 -*- """ @File :demo5.py @Author :葉庭云 @CSDN :https://yetingyun.blog.csdn.net/ """ import pandas as pd from pyecharts.charts import Geo from pyecharts import options as opts from pyecharts.globals import GeoType, CurrentConfig, ThemeType CurrentConfig.ONLINE_HOST = 'D:/python/pyecharts-assets-master/assets/' # 讀取Excel數(shù)據(jù) 數(shù)據(jù)來(lái)源美團(tuán)網(wǎng)酒店信息 df = pd.read_excel("hotel.xlsx") # 獲取 地點(diǎn) 經(jīng)緯度信息 geo_sight_coord = {df.iloc[i]['酒店地址']: [df.iloc[i]['經(jīng)度'], df.iloc[i]['緯度']] for i in range(len(df))} data = [(df['酒店地址'][j], f"{int(df['最低價(jià)'][j])}元(最低價(jià))") for j in range(len(df))] # print(data) # print(geo_sight_coord) # 實(shí)例化Geo對(duì)象 導(dǎo)入成都地圖 g = Geo(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION, width="1000px", height="600px")) g.add_schema(maptype="成都") for k, v in list(geo_sight_coord.items()): # 添加地址、經(jīng)緯度數(shù)據(jù) g.add_coordinate(k, v[0], v[1]) # 生成漣漪散點(diǎn)圖 g.add("", data_pair=data, type_=GeoType.EFFECT_SCATTER, symbol_size=6) g.set_series_opts(label_opts=opts.LabelOpts(is_show=False)) g.set_global_opts(title_opts=opts.TitleOpts(title="成都-酒店地址分布")) g.render("酒店地址分布.html")
運(yùn)行效果如下:
到此這篇關(guān)于一文帶你掌握Pyecharts地理數(shù)據(jù)可視化的方法的文章就介紹到這了,更多相關(guān)Pyecharts地理數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 計(jì)算積分圖和haar特征的實(shí)例代碼
今天小編就為大家分享一篇python 計(jì)算積分圖和haar特征的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11vue+element-ui動(dòng)態(tài)加載本地圖片方式
這篇文章主要介紹了vue+element-ui動(dòng)態(tài)加載本地圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09python pandas寫(xiě)入excel文件的方法示例
這篇文章主要介紹了python pandas寫(xiě)入excel文件的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06解決python3 urllib中urlopen報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了關(guān)于解決python3 urllib中urlopen報(bào)錯(cuò)問(wèn)題的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03如何用Python 實(shí)現(xiàn)全連接神經(jīng)網(wǎng)絡(luò)(Multi-layer Perceptron)
這篇文章主要介紹了如何用Python 實(shí)現(xiàn)全連接神經(jīng)網(wǎng)絡(luò)(Multi-layer Perceptron),幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10Python求兩個(gè)字符串最長(zhǎng)公共子序列代碼實(shí)例
這篇文章主要介紹了Python求兩個(gè)字符串最長(zhǎng)公共子序列代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03matplotlib.pyplot畫(huà)圖 圖片的二進(jìn)制流的獲取方法
今天小編就為大家分享一篇matplotlib.pyplot畫(huà)圖 圖片的二進(jìn)制流的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05python?pygame實(shí)現(xiàn)打磚塊游戲
這篇文章主要為大家詳細(xì)介紹了python?pygame實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Jupyter安裝拓展nbextensions及解決官網(wǎng)下載慢的問(wèn)題
這篇文章主要介紹了Jupyter安裝拓展nbextensions及解決官網(wǎng)下載慢的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03