0

0

获取Plotly Hexbin Mapbox热图中每个六边形的GPS边界

碧海醫心

碧海醫心

发布时间:2025-08-08 16:32:01

|

1021人浏览过

|

来源于php中文网

原创

获取plotly hexbin mapbox热图中每个六边形的gps边界

本文档详细介绍了如何从Plotly的hexbin_mapbox热图中提取每个六边形的信息,包括平均值、中心点GPS坐标以及六个角点的GPS坐标。我们将使用geopandas库处理地理空间数据,并将其转换为pandas DataFrame,方便后续分析和使用。

从Plotly Figure中提取数据

首先,我们需要从Plotly生成的fig对象中提取六边形的坐标和值。这些信息存储在fig.data[0]中。

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
import numpy as np
import plotly.figure_factory as ff

# 示例数据(与问题中的数据相同)
gps_coordinates = [[32.7792, -96.7959, 10000], 
                  [32.7842, -96.7920, 15000], 
                  [32.8021, -96.7819, 12000], 
                  [32.7916, -96.7833, 26000], 
                  [32.7842, -96.7920, 51000],
                  [32.7842, -96.7920, 17000], 
                  [32.7792, -96.7959, 25000], 
                  [32.7842, -96.7920, 19000], 
                  [32.7842, -96.7920, 31000], 
                  [32.7842, -96.7920, 40000]]

df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])

fig = ff.create_hexbin_mapbox(
      data_frame=df, lat='LATITUDE', lon='LONGITUDE',
      nx_hexagon=2, 
      opacity=0.2, 
      labels={"color": "Dollar Value"},
      color='Value',
      agg_func=np.mean, 
      color_continuous_scale="Jet",
      zoom=14,
      min_count=1, # This gets rid of boxes for which we have no data
      height=900,
      width=1600,
      show_original_data=True,
      original_data_marker=dict(size=5, opacity=0.6, color="deeppink"),
      )

fig.update_layout(mapbox_style="open-street-map")

# 提取坐标和值
coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]
values = fig.data[0]['z']

创建GeoPandas DataFrame

接下来,我们将使用提取的坐标和值创建一个GeoPandas DataFrame。GeoPandas扩展了Pandas的功能,增加了对地理空间数据的支持。

# 创建DataFrame
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})

# 创建几何对象
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

# 创建GeoDataFrame
hexbins_gdf = gpd.GeoDataFrame(hexbins_df, geometry='geometry')

获取六边形的中心点

GeoPandas提供了方便的方法来计算几何对象的中心点。我们可以直接使用centroid属性来获取每个六边形的中心点坐标。

如此AI员工
如此AI员工

国内首个全链路营销获客AI Agent

下载
# 计算中心点
hexbins_gdf['centroid'] = hexbins_gdf['geometry'].centroid

获取六边形的角点坐标

为了获取每个六边形的角点坐标,我们可以将坐标列表转换为单独的列。由于每个六边形有六个角点,我们需要创建六个新列。

# 提取角点坐标
corners_df = hexbins_gdf['coordinates'].apply(lambda x: pd.Series(x[0])).rename(columns=lambda x: f'corner_{x+1}')

# 合并角点坐标到DataFrame
hexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # 删除第七个角点,因为它与第一个角点相同

完整代码示例

以下是完整的代码示例,包括数据准备、数据提取、GeoPandas DataFrame创建、中心点计算和角点坐标提取。

import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
import numpy as np
import plotly.figure_factory as ff

# 示例数据(与问题中的数据相同)
gps_coordinates = [[32.7792, -96.7959, 10000], 
                  [32.7842, -96.7920, 15000], 
                  [32.8021, -96.7819, 12000], 
                  [32.7916, -96.7833, 26000], 
                  [32.7842, -96.7920, 51000],
                  [32.7842, -96.7920, 17000], 
                  [32.7792, -96.7959, 25000], 
                  [32.7842, -96.7920, 19000], 
                  [32.7842, -96.7920, 31000], 
                  [32.7842, -96.7920, 40000]]

df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])

fig = ff.create_hexbin_mapbox(
      data_frame=df, lat='LATITUDE', lon='LONGITUDE',
      nx_hexagon=2, 
      opacity=0.2, 
      labels={"color": "Dollar Value"},
      color='Value',
      agg_func=np.mean, 
      color_continuous_scale="Jet",
      zoom=14,
      min_count=1, # This gets rid of boxes for which we have no data
      height=900,
      width=1600,
      show_original_data=True,
      original_data_marker=dict(size=5, opacity=0.6, color="deeppink"),
      )

fig.update_layout(mapbox_style="open-street-map")

# 提取坐标和值
coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]
values = fig.data[0]['z']

# 创建DataFrame
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values})

# 创建几何对象
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

# 创建GeoDataFrame
hexbins_gdf = gpd.GeoDataFrame(hexbins_df, geometry='geometry')

# 计算中心点
hexbins_gdf['centroid'] = hexbins_gdf['geometry'].centroid

# 提取角点坐标
corners_df = hexbins_gdf['coordinates'].apply(lambda x: pd.Series(x[0])).rename(columns=lambda x: f'corner_{x+1}')

# 合并角点坐标到DataFrame
hexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # 删除第七个角点,因为它与第一个角点相同

# 打印结果
print(hexbins_df)

注意事项

  • 依赖库: 确保安装了pandas, geopandas, shapely, plotly。 可以使用pip install pandas geopandas shapely plotly 命令安装。
  • 坐标系: GeoPandas DataFrame默认使用WGS 84坐标系(EPSG:4326)。如果需要,可以将其转换为其他坐标系。
  • 数据量: 处理大量六边形数据时,性能可能会受到影响。可以考虑使用空间索引等技术来提高性能。
  • 六边形方向: shapely.geometry.LineString创建的几何对象可能需要进一步处理,以确保其方向符合预期。

总结

通过使用Plotly、GeoPandas和Shapely,我们可以方便地从hexbin_mapbox热图中提取每个六边形的平均值、中心点坐标和角点坐标,并将其转换为易于分析和使用的DataFrame。这个过程为地理空间数据的分析和可视化提供了强大的工具

相关专题

更多
Python 时间序列分析与预测
Python 时间序列分析与预测

本专题专注讲解 Python 在时间序列数据处理与预测建模中的实战技巧,涵盖时间索引处理、周期性与趋势分解、平稳性检测、ARIMA/SARIMA 模型构建、预测误差评估,以及基于实际业务场景的时间序列项目实操,帮助学习者掌握从数据预处理到模型预测的完整时序分析能力。

53

2025.12.04

pip安装使用方法
pip安装使用方法

安装步骤:1、确保Python已经正确安装在您的计算机上;2、下载“get-pip.py”脚本;3、按下Win + R键,然后输入cmd并按下Enter键来打开命令行窗口;4、在命令行窗口中,使用cd命令切换到“get-pip.py”所在的目录;5、执行安装命令;6、验证安装结果即可。大家可以访问本专题下的文章,了解pip安装使用方法的更多内容。

339

2023.10.09

更新pip版本
更新pip版本

更新pip版本方法有使用pip自身更新、使用操作系统自带的包管理工具、使用python包管理工具、手动安装最新版本。想了解更多相关的内容,请阅读专题下面的文章。

411

2024.12.20

pip设置清华源
pip设置清华源

设置方法:1、打开终端或命令提示符窗口;2、运行“touch ~/.pip/pip.conf”命令创建一个名为pip的配置文件;3、打开pip.conf文件,然后添加“[global];index-url = https://pypi.tuna.tsinghua.edu.cn/simple”内容,这将把pip的镜像源设置为清华大学的镜像源;4、保存并关闭文件即可。

757

2024.12.23

python升级pip
python升级pip

本专题整合了python升级pip相关教程,阅读下面的文章了解更多详细内容。

348

2025.07.23

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

6

2026.01.22

html编辑相关教程合集
html编辑相关教程合集

本专题整合了html编辑相关教程合集,阅读专题下面的文章了解更多详细内容。

47

2026.01.21

三角洲入口地址合集
三角洲入口地址合集

本专题整合了三角洲入口地址合集,阅读专题下面的文章了解更多详细内容。

24

2026.01.21

AO3中文版入口地址大全
AO3中文版入口地址大全

本专题整合了AO3中文版入口地址大全,阅读专题下面的的文章了解更多详细内容。

314

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 2.9万人学习

Git版本控制工具
Git版本控制工具

共8课时 | 1.5万人学习

Git中文开发手册
Git中文开发手册

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号