Plotly Choropleth地图:如何将1.425887B格式化为1.4B?
Plotly Choropleth地图悬停人口数值格式化方案
问题背景
绘制世界人口Choropleth地图时,鼠标悬停显示的人口数值(如中国的1.425887B)需要格式化为保留一位小数的形式(如1.4B)。当前实现代码及数据集结构如下:
当前实现代码
data2022 = dict(type = 'choropleth', colorscale = 'agsunset', reversescale = True, locations = df['Country/Territory'], locationmode = 'country names', z = df['2022 Population'], text = df['CCA3'], marker = dict(line = dict(color = 'rgb(12, 12, 12)', width=1)), colorbar = {'title': 'Population'}) layout2022 = dict(title = '<b>World Population 2022<b>', geo = dict(showframe = True, showland = True, landcolor = 'rgb(198, 197, 198)', showlakes = True, lakecolor = 'rgb(85, 173, 240)', showrivers = True, rivercolor = 'rgb(173, 216, 230)', showocean = True, oceancolor = 'rgb(173, 216, 230)', projection = {'type': 'natural earth'})) choromap2022 = go.Figure(data=[data2022], layout=layout2022) choromap2022.update_geos(lataxis_showgrid = True, lonaxis_showgrid = True) choromap2022.update_layout(height = 600, title_x = 0.5, title_font_color = 'red', title_font_family = 'Times New Roman', title_font_size = 30, margin=dict(t=80, r=50, l=50)) iplot(choromap2022)
数据集结构
'CCA3': {0: 'AFG', 1: 'ALB', 2: 'DZA', 3: 'ASM', 4: 'AND'}, 'Country/Territory': {0: 'Afghanistan', 1: 'Albania', 2: 'Algeria', 3: 'American Samoa', 4: 'Andorra'}, 'Capital': {0: 'Kabul', 1: 'Tirana', 2: 'Algiers', 3: 'Pago Pago', 4: 'Andorra la Vella'}, 'Continent': {0: 'Asia', 1: 'Europe', 2: 'Africa', 3: 'Oceania', 4: 'Europe'}, '2022 Population': {0: 41128771, 1: 2842321, 2: 44903225, 3: 44273, 4: 79824}, '2020 Population': {0: 38972230, 1: 2866849, 2: 43451666, 3: 46189, 4: 77700}, '2015 Population': {0: 33753499, 1: 2882481, 2: 39543154, 3: 51368, 4: 71746}, '2010 Population': {0: 28189672, 1: 2913399, 2: 35856344, 3: 54849, 4: 71519}, '2000 Population': {0: 19542982, 1: 3182021, 2: 30774621, 3: 58230, 4: 66097}, '1990 Population': {0: 10694796, 1: 3295066, 2: 25518074, 3: 47818, 4: 53569}, '1980 Population': {0: 12486631, 1: 2941651, 2: 18739378, 3: 32886, 4: 35611}, '1970 Population': {0: 10752971, 1: 2324731, 2: 13795915, 3: 27075, 4: 19860}, 'Area (km²)': {0: 652230, 1: 28748, 2: 2381741, 3: 199, 4: 468}, 'Density (per km²)': {0: 63.0587, 1: 98.8702, 2: 18.8531, 3: 222.4774, 4: 170.5641}, 'Growth Rate': {0: 1.0257, 1: 0.9957, 2: 1.0164, 3: 0.9831, 4: 1.01}, 'World Population Percentage': {0: 0.52, 1: 0.04, 2: 0.56, 3: 0.0, 4: 0.0}
解决方案
通过自定义hovertemplate参数可以实现悬停数值的格式化,以下提供两种可行方法:
方法一:利用Plotly内置格式化语法(简洁高效)
直接在data2022中添加hovertemplate,使用Plotly内置的数值格式化规则将人口数转为十亿单位并保留一位小数:
data2022 = dict(type='choropleth', colorscale='agsunset', reversescale=True, locations=df['Country/Territory'], locationmode='country names', z=df['2022 Population'], text=df['CCA3'], # 自定义悬停模板,%{z:.1s}自动将数值转为缩写形式并保留一位有效数字 hovertemplate='<b>%{location}</b><br>' + 'CCA3: %{text}<br>' + 'Population: %{z:.1s}B<extra></extra>', marker=dict(line=dict(color='rgb(12, 12, 12)', width=1)), colorbar={'title': 'Population'})
方法二:预先格式化数值(灵活性更高)
先通过函数将人口数转为指定格式的字符串,再通过customdata传入悬停模板:
# 定义格式化函数,将人口数转为保留一位小数的十亿单位字符串 def format_population(pop): return f"{pop / 1e9:.1f}B" data2022 = dict(type='choropleth', colorscale='agsunset', reversescale=True, locations=df['Country/Territory'], locationmode='country names', z=df['2022 Population'], text=df['CCA3'], # 自定义悬停模板,调用customdata中的格式化后数值 hovertemplate='<b>%{location}</b><br>' + 'CCA3: %{text}<br>' + 'Population: %{customdata}<extra></extra>', # 传入预先格式化好的人口数值 customdata=df['2022 Population'].apply(format_population), marker=dict(line=dict(color='rgb(12, 12, 12)', width=1)), colorbar={'title': 'Population'})
替换data2022部分后重新运行代码,即可看到悬停时人口数值显示为1.4B这类保留一位小数的格式。
内容的提问来源于stack exchange,提问作者Đạt Dương
相关产品推荐
相关产品推荐

