如何在GeoPandas Pyplot图例中添加千分符或显示百万单位?
解决GeoPandas图例数值格式化问题
1. 显示带千分符的数值(100,000,000)
直接修改legend_kwds中的format参数,使用Python千分符格式化语法即可实现:
df_test.plot( ax=ax, zorder=1, cax=cax, column='tax', cmap='YlOrRd', legend=True, legend_kwds={'label': '(USD)', 'orientation': "vertical", 'pad': 0.3, 'format': '{:,.0f}'}, # 替换为千分符格式 );
2. 显示百万单位格式(100mill)
自定义一个格式化函数,将数值转换为百万单位后拼接后缀,再传入format参数:
def format_millions(x): # 将数值转换为百万单位并拼接后缀 return f'{int(x / 1000000)}mill' df_test.plot( ax=ax, zorder=1, cax=cax, column='tax', cmap='YlOrRd', legend=True, legend_kwds={'label': '(USD)', 'orientation': "vertical", 'pad': 0.3, 'format': format_millions}, # 传入自定义格式化函数 );
内容的提问来源于stack exchange,提问作者eetiaro
相关产品推荐
相关产品推荐

