1、分级着色地图
分级着色地图常用于可视化地理数据,比如人口密度、经济数据、气候变化等。其原理是使用颜色或阴影的渐变来表示不同区域(如国家、省份、城市等)中的数据差异。例如,地图上的每个区域根据其代表的数值被着色,通常数值越大,颜色越深。
2、案例1
import plotly.express as px
import pandas as pd
# 将数据转化为pandas DataFrame
data = {
'Country': ['Australia', 'United States', 'Brazil', 'Russia', 'India', 'South Africa', ],
'Value': [100, 90, 80, 70, 85, 95]
}
df = pd.DataFrame(data)
# 创建Choropleth地图
fig = px.choropleth(
df,
locations='Country',
locationmode='country names',
color='Value', color_continuous_scale='Blues',
title='分级着色地图'
)
# 显示地图
fig.show()
运行结果:
2、案例2
import pandas as pd
import plotly.express as px
import numpy as np
import json
with open("china_province.geojson", encoding='utf8') as f:
provinces_map = json.load(f)
df = pd.read_csv('data.csv')
df.确诊 = df.确诊.map(np.log) # 由于各省确诊数据差距较大,数据尺度大,将所有数据采用对数形式
print(df.确诊)
fig = px.choropleth_mapbox(
df,
geojson=provinces_map,
color='确诊',
locations="地区",
featureidkey="properties.NL_NAME_1",
mapbox_style="carto-darkmatter",
color_continuous_scale='viridis',
center={"lat": 37.110573, "lon": 106.493924},
zoom=3,
hover_name="地区",
hover_data=["确诊", "疑似", "治愈", "死亡"],
)
# fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.show()
运行结果:
参考资料
https://blog.csdn.net/u010099080/article/details/104543491
https://github.com/secsilm/plotly-choropleth-mapbox-demo