
本文档详细介绍了如何从Plotly的hexbin_mapbox热图中提取每个六边形的信息,包括平均值、中心点GPS坐标以及六个角点的GPS坐标。我们将使用geopandas库处理地理空间数据,并将其转换为pandas DataFrame,方便后续分析和使用。
首先,我们需要从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扩展了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属性来获取每个六边形的中心点坐标。
# 计算中心点 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)通过使用Plotly、GeoPandas和Shapely,我们可以方便地从hexbin_mapbox热图中提取每个六边形的平均值、中心点坐标和角点坐标,并将其转换为易于分析和使用的DataFrame。这个过程为地理空间数据的分析和可视化提供了强大的工具。
以上就是获取Plotly Hexbin Mapbox热图中每个六边形的GPS边界的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号