關(guān)于Python可視化Dash工具—散點(diǎn)地圖、熱力地圖、線形地圖
好不容易實(shí)現(xiàn)了choropleth_mapbox地圖,也順道把散點(diǎn)地圖、熱力地圖、線形地圖處理掉吧,做到有始有終,再邁向新的領(lǐng)域;從微信公眾號(hào)里拿到了按分省統(tǒng)計(jì)的用戶數(shù)據(jù),又拿到了各地市數(shù)據(jù),通過這兩種數(shù)據(jù)分別實(shí)現(xiàn)choropleth_mapbox、scatter_mapbox、density_mapbox,至于line_mapbox構(gòu)造數(shù)據(jù)過于麻煩,直接拷貝了官網(wǎng)上的案例。
基于分省統(tǒng)計(jì)的用戶數(shù)據(jù),通過choropleth_mapbox進(jìn)行行政區(qū)域的數(shù)據(jù)展示。
import json
import pandas as pd
import plotly.express as px
# 中國(guó)地圖
with open('china_geo.json') as response:
counties = json.load(response)
df = pd.read_csv("gongzhonghaopro.csv",encoding="utf-8",
dtype={"areacode": str})
fig = px.choropleth_mapbox(df, geojson=counties, featureidkey="properties.adcode",locations='areacode', color='total',
#color_continuous_scale="Viridis",
range_color=(0, 320),
color_continuous_scale='Reds',
zoom=3, center={"lat": 37.4189, "lon": 116.4219},
mapbox_style='carto-positron')
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()
??
基于分地市統(tǒng)計(jì)的用戶數(shù)據(jù),通過scatter_mapbox進(jìn)行各城市的數(shù)據(jù)展示。這里面不同的地方是直接采用mapbox的地圖,而且申請(qǐng)了一個(gè)tokenkey,此外數(shù)據(jù)的構(gòu)成方面,需要附上各區(qū)域的經(jīng)度、維度、展示數(shù)據(jù)等信息。
all_data = pd.read_csv("gongzhonghao.csv",encoding="utf-8")
token = 'pk.eyJ1Ijoiam9obndhbmcyMDIxIiwiYSI6ImNrbWNheTJ1NzA4cWQyb21uZHYycTgzMGQifQ.Tyk26CpuPLPi3bpw05yM_A'
fig = px.scatter_mapbox(all_data, lat="langitude", lon="latitude", hover_name="city", hover_data=["city", "total"],
color="total",
size="total",
#color_continuous_scale=px.colors.sequential.matter,
size_max=30,
color_continuous_scale="Reds",
zoom=5, height=1000)
fig.update_layout(mapbox = {'accesstoken': token, #需要到官網(wǎng)注冊(cè)一個(gè)token
'center': {'lon': 106.573, 'lat': 30.66342}, #指定的地圖中心
'zoom': 3,
'style': 'basic', #顯示的地圖類型,有遙感地圖,街道地圖等類型
},
margin = {'l': 0, 'r': 0, 't': 0, 'b': 0})
fig.show()
基于分地市統(tǒng)計(jì)的用戶數(shù)據(jù),通過density_mapbox進(jìn)行各城市的數(shù)據(jù)展示。熱力圖相對(duì)來說呈現(xiàn)效果要好于scatter_mapbox,不過這里面關(guān)于range_color的設(shè)置一直沒想明白該如何正確處理。
all_data = pd.read_csv("gongzhonghao.csv",encoding="utf-8")
print(all_data)
fig = px.density_mapbox(all_data, lat='langitude', lon='latitude', z='total', radius=20,
color_continuous_scale=px.colors.diverging.RdYlGn[::-1],
center={"lat": 37.4189, "lon": 116.4219}, zoom=3,
range_color =(0,15),
mapbox_style="stamen-terrain")
fig.show()
??
基于官方案例的line_geo地圖展示。
下一步該探索一下dash_core_components 、dash_html_components 、idash_bootstrap_components組件了,最終的目標(biāo)是實(shí)現(xiàn)可視化大屏,還有很長(zhǎng)的路要走。