当前位置: 首页 > news >正文

Python 在世界地图上加气泡图

V1

import plotly.express as px
import pandas as pd# 数据准备
data = {"Country": ["Australia", "USA", "Germany", "South Korea", "New Zealand","China", "Japan", "Denmark", "Canada", "UK", "Italy","Singapore", "Netherlands", "Switzerland", "France", "Austria","South Africa", "Poland", "Portugal", "Sweden", "Croatia", "Greece"],"Value": [20, 19, 17, 16, 8, 6, 6, 5, 5, 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]
}df = pd.DataFrame(data)# 创建气泡地图
fig = px.scatter_geo(df,locations="Country",locationmode="country names",color="Value",size="Value",hover_name="Country",projection="natural earth",color_continuous_scale=px.colors.sequential.Plasma,title="World Map with Value-based Bubbles",size_max=30)# 自定义布局
fig.update_geos(showcountries=True, showcoastlines=True, showland=True,landcolor="lightgray", countrycolor="white")
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})fig.show()

V2  标注国家及数理

import plotly.express as px
import pandas as pd# 数据准备
data = {"Country": ["Australia", "USA", "Germany", "South Korea", "New Zealand","China", "Japan", "Denmark", "Canada", "UK", "Italy","Singapore", "Netherlands", "Switzerland", "France", "Austria","South Africa", "Poland", "Portugal", "Sweden", "Croatia", "Greece"],"Value": [20, 19, 17, 16, 8, 6, 6, 5, 5, 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]
}df = pd.DataFrame(data)
df["Label"] = df["Country"] + " (" + df["Value"].astype(str) + ")"  # 创建标注文本# 创建气泡地图
fig = px.scatter_geo(df,locations="Country",locationmode="country names",color="Value",size="Value",hover_name="Country",text="Label",  # 添加文本标注projection="natural earth",color_continuous_scale=px.colors.diverging.Tealrose,title="World Value Distribution with Annotations",size_max=25)# 自定义样式
fig.update_traces(textposition="top center",  # 文本位置textfont=dict(family="Arial",size=12,color="black"),marker=dict(line=dict(width=0.5, color="gray"),  # 气泡边框opacity=0.8)
)# 优化地理特征
fig.update_geos(showcountries=True,countrycolor="lightgray",showcoastlines=False,showland=True,landcolor="white",showocean=True,oceancolor="azure"
)# 调整布局
fig.update_layout(margin={"r":20,"t":50,"l":20,"b":20},coloraxis_colorbar=dict(title="Value Scale",thickness=20,len=0.75),hoverlabel=dict(bgcolor="white",font_size=14)
)fig.show()

V3 标尺字体大小修改

import plotly.express as px
import pandas as pd# 数据准备
data = {"Country": ["Australia", "USA", "Germany", "South Korea", "New Zealand","China", "Japan", "Denmark", "Canada", "UK", "Italy","Singapore", "Netherlands", "Switzerland", "France", "Austria","South Africa", "Poland", "Portugal", "Sweden", "Croatia", "Greece"],"Value": [20, 19, 17, 16, 8, 6, 6, 5, 5, 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1]
}df = pd.DataFrame(data)
df["Label"] = df["Country"] + " (" + df["Value"].astype(str) + ")"  # 创建标注文本# 创建气泡地图
fig = px.scatter_geo(df,locations="Country",locationmode="country names",color="Value",size="Value",hover_name="Country",text="Label",projection="natural earth",color_continuous_scale=px.colors.diverging.Tealrose,title="World Value Distribution with Annotations",size_max=50)# 自定义样式
fig.update_traces(textposition="top center",textfont=dict(family="Arial",size=22,color="black"),marker=dict(line=dict(width=0.5, color="gray"),opacity=0.8)
)# 优化地理特征
fig.update_geos(showcountries=True,countrycolor="lightgray",showcoastlines=False,showland=True,landcolor="white",showocean=True,oceancolor="azure"
)# 调整布局(重点修改部分)
fig.update_layout(margin={"r": 20, "t": 50, "l": 20, "b": 20},coloraxis_colorbar=dict(title="Value",thickness=30,len=0.65,# 新增字体设置tickfont=dict(    # 刻度数字字体size=25,      # 从默认12放大到14family="Arial")),hoverlabel=dict(bgcolor="white",font_size=30)
)fig.show()

http://www.xdnf.cn/news/253333.html

相关文章:

  • 【多线程】六、基于阻塞队列的生产者消费者模型
  • react js 查看字体效果
  • MySQL 中的游标(Cursor)
  • NV162NV172美光固态颗粒NV175NV188
  • SpringBoot癌症患者交流平台设计开发
  • Flutter AppBar 详解
  • gRPC学习笔记记录以及整合gin开发
  • 【云备份】配置文件加载模块
  • 贝叶斯算法(Bayesian Algorithms)详解
  • DBeaver连接人大金仓数据库V9
  • Nginx搭建test服务器
  • 企业级分布式 MCP 方案
  • 文章六:《循环神经网络(RNN)与自然语言处理》
  • 第十六届蓝桥杯 2025 C/C++组 客流量上限
  • 2025五一数学建模竞赛A题完整分析论文(共45页)(含模型、可运行代码、数据)
  • 【服务器通信-socket】——int socket(int domain, int type, int protocol);
  • LangChain入门(五)AI记住聊天历史
  • Android基础控件用法介绍
  • 报文三次握手对么٩(๑^o^๑)۶
  • 开源ERP系统对比:Dolibarr、ERPNext与Odoo
  • 【每日八股】复习 Redis Day5:集群(上)
  • 云原生后端:构建高效、可扩展的现代后端架构
  • HBM的哪些事
  • 【凑修电脑的小记录】vscode打不开
  • React useCallback函数
  • 从爬虫到网络---<基石3> gfw防火墙是怎么保护我们的?
  • Linux系统:进程程序替换以及相关exec接口
  • CMake separate_arguments用法详解
  • Trae 安装第三方插件支持本地部署的大语言模型
  • Matlab自学笔记