我已经使用Ploly在Python语言中创建了一个六进制的"热图",通过映射多个位置(使用GPS纬度/经度)以及每个位置的值.请参见下面的代码,了解示例df和hexbin图绘制.

Data Desired

当我将鼠标放在每个六角箱上时,我可以看到该六角箱中包含的平均值.但我想要的是一种方法,可以将每个六角箱的以下信息下载到Pandas DF中:

  • 每个六进制框的平均值(已经根据下面的代码计算,但目前我只能通过将鼠标移到每个六进制框上才能访问;我希望能够将其下载到DF中)
  • 每个六边形的质心GPS坐标
  • 六角框的每个角的GPS坐标(即,每个六角框的每一个角的纬度和经度)

My Question

我如何才能将上述项目中描述的数据下载到Pandas DF中?

Code example

# Import dependencies
import pandas as pd
import numpy as np
import plotly.figure_factory as ff
import plotly.express as px

# Create a list of GPS coordinates
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]]

# Create a DataFrame with the GPS coordinates
df = pd.DataFrame(gps_coordinates, columns=['LATITUDE', 'LONGITUDE', 'Value'])

# Print the DataFrame
display(df)
# Create figure using 'df_redfin_std_by_year_and_acreage_bin' data
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"),
      )

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

fig.show()

推荐答案

您可以提取每个hexbin的六个角的坐标以及从fig.data[0]开始的值.但是,我不确定质心信息存储在figure对象中的何处,但我们可以从这些数据创建一个mixandas mixrame,并直接获取几何列的centroids属性:

将地理Pandas 作为gpd导入 从shapely.geometry导入线串

coordinates = [feature['geometry']['coordinates'] for feature in fig.data[0].geojson['features']]
values = fig.data[0]['z']
hexbins_df = pd.DataFrame({'coordinates': coordinates, 'values': values}) 
hexbins_df['geometry'] = hexbins_df['coordinates'].apply(lambda x: LineString(x[0]))

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}')
hexbins_df = pd.concat([hexbins_df, corners_df], axis=1).drop(columns='corner_7') # we drop corner_7 since that is the same as the starting corner

生成的地理Pandas 数据帧如下所示:

>>> hexbins_df
                                         coordinates        values  ...                                 corner_5                                 corner_6
0  [[[-96.7889, 32.78215666477984], [-96.78539999...  28833.333333  ...     [-96.792400000007, 32.7872532054738]    [-96.792400000007, 32.78385554412095]
1  [[[-96.792400000007, 32.777059832108314], [-96...  17500.000000  ...  [-96.79590000001399, 32.78215666477984]  [-96.79590000001399, 32.77875880877266]
2  [[[-96.785399999993, 32.7872532054738], [-96.7...  26000.000000  ...            [-96.7889, 32.79234945416662]           [-96.7889, 32.788951987483806]
3  [[[-96.785399999993, 32.79744541083471], [-96....  12000.000000  ...            [-96.7889, 32.80254107545448]            [-96.7889, 32.79914399815894]

[4 rows x 21 columns]

Python相关问答推荐

使用图片生成PDF Django rest框架

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

如何通过多2多字段过滤查询集

如何在Python中使用io.BytesIO写入现有缓冲区?

通过优化空间在Python中的饼图中添加标签

Python在tuple上操作不会通过整个单词匹配

Matlab中是否有Python的f-字符串等效物

'discord.ext. commanders.cog没有属性监听器'

如何标记Spacy中不包含特定符号的单词?

未删除映射表的行

OR—Tools CP SAT条件约束

SQLAlchemy Like ALL ORM analog

创建可序列化数据模型的最佳方法

为一个组的每个子组绘制,

mypy无法推断类型参数.List和Iterable的区别

启用/禁用shiny 的自动重新加载

Pandas Data Wrangling/Dataframe Assignment

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?

PYTHON、VLC、RTSP.屏幕截图不起作用

Polars Group by描述扩展