如何对经度和纬度进行分箱并绘制分箱的密度?

问题描述

我正在测试以下代码

import numpy as np
import pandas as pd
#import matplotlib.pyplot as plt
#plt.style.use('seaborn-white')


df = pd.read_csv('C:\\Users\\ryans\\OneDrive\\Desktop\\business.csv')
X = df[['latitude','longitude','address']].copy()

X['latitude'].value_counts()
X['longitude'].value_counts()

结果:

-115.123695    168
-111.940325    167
-115.171130    158
-111.821087    157
-115.224485    156

-82.032188       1
-89.383229       1
-89.533178       1
-81.475399       1
-111.857103      1

下一步...

X['lat'] = pd.cut(df['latitude'],bins=10)
X['lon'] = pd.cut(df['longitude'],bins=10)
print(X)

结果:

         latitude   longitude  ...               lat                   lon
0       33.522143 -112.018481  ...  (33.187,35.014]  (-115.536,-111.235]
1       43.605499  -79.652289  ...  (42.252,44.062]     (-81.428,-77.17]
2       35.092564  -80.859132  ...  (35.014,36.824]     (-81.428,-77.17]
3       33.455613 -112.395596  ...  (33.187,-111.235]
4       35.190012  -80.887223  ...  (35.014,-77.17]
          ...         ...  ...               ...                   ...
192604  36.213732 -115.177059  ...  (35.014,36.824]  (-115.536,-111.235]
192605  44.052658  -79.481850  ...  (42.252,-77.17]
192606  33.679992 -112.035569  ...  (33.187,-111.235]
192607  33.416137 -111.735743  ...  (33.187,-111.235]
192608  36.107267 -115.171920  ...  (35.014,-111.235]

现在,我正在尝试可视化这些坐标箱,并绘制这些箱的密度。因此,计数越高,颜色越浓。有可能吗?

我在网上找到了两个示例,这些示例显示了如何创建经度和纬度数据的热图。这是唯一的办法吗,还是可以对这些数据点进行装箱?

解决方法

下面的代码做的很好!

import pandas as pd 
import folium
from folium.plugins import HeatMap


df = pd.read_csv('C:\\your_path\\business.csv')
df.head(3)

max_amount = float(df['review_count'].max())

hmap = folium.Map(location=[42.5,-75.5],zoom_start=7,)

hm_wide = HeatMap( list(zip(df.latitude.values,df.longitude.values,df.review_count.values)),min_opacity=0.2,max_val=max_amount,radius=17,blur=15,max_zoom=1,)

hmap.add_child(hm_wide)

enter image description here

enter image description here

我需要处理其中一些颜色的不透明度和强度,但是这个概念绝对是合理的。