出租车轨迹数据分析:热力图与热点识别
该代码主要对出租车轨迹数据进行处理和分析,具体流程如下:
-
获取出租车轨迹数据中经度和纬度的最大最小值,确定轨迹点的区域范围。
-
筛选出在区域范围内的轨迹点,用于后续的热力图和热点分析。
-
绘制热力图,用于展示轨迹点的密度分布情况。
-
根据轨迹数据生成每个轨迹的经纬度信息,用于后续的热点分析。
-
将整个区域划分为若干个网格,统计每个网格内的出租车数量。
-
根据设定的阈值,找出出租车数量超过阈值的网格,标记为热点网格。
-
绘制热点交互网络,用于展示热点网格的分布情况。
总体来说,该代码实现了对出租车轨迹数据的处理和分析,并通过热力图和热点分析展示了出租车轨迹点的密度和热点分布情况。
lat1 = df['纬度'].min()
lon1 = df['经度'].min()
lat2 = df['纬度'].max()
lon2 = df['经度'].max()
# 筛选出在区域范围内的轨迹点
df = df[(df['纬度'] >= lat1) & (df['纬度'] <= lat2)]
df = df[(df['经度'] >= lon1) & (df['经度'] <= lon2)]
# 绘制热力图
fig, ax = plt.subplots(figsize=(10, 8))
heatmap, xedges, yedges = np.histogram2d(df['纬度'], df['经度'], bins=100)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
im = ax.imshow(heatmap.T, extent=extent, origin='lower', cmap='YlOrRd')
cb = fig.colorbar(im, ax=ax)
# cb.set_label('Density')
ax.set_xlabel('Latitude')
ax.set_ylabel('Longitude')
plt.show()
trajectories = []
lats = df['纬度'].tolist()
lngs = df['经度'].tolist()
traj = df['轨迹ID'].tolist()
def gen_location(i):
lat = lats[i]
lng = lngs[i]
return lng, lat
def gen_trajectory(i):
trajectory = []
for j in range(len(traj)):
if traj[j] == i:
location = gen_location(j)
trajectory.append((location[0], location[1]))
return trajectory
num_trajectories = len(set(traj))
for i in range(num_trajectories):
trajectory = gen_trajectory(i)
trajectories.append(trajectory)
trajectories
# 统计每个网格内的出租车数量
num_grid_x = 200
num_grid_y = 100
grid_size_x = 0.01
grid_size_y = 0.01
grid_counts = [[0 for j in range(num_grid_y)] for i in range(num_grid_x)]
for trajectory in trajectories:
for point in trajectory:
lng = point[0]
lat = point[1]
x = int((lng - lon1) / grid_size_x)
y = int((lat - lat1) / grid_size_y)
if x >= 0 and x < num_grid_x and y >= 0 and y < num_grid_y:
grid_counts[x][y] += 1
# 找出热点网格
hotspot_threshold = 10 # 出租车数量阈值
hotspots = []
for x in range(num_grid_x):
for y in range(num_grid_y):
count = grid_counts[x][y]
if count > hotspot_threshold:
lng = lon1 + grid_size_x * (x + 0.5)
lat = lat1 + grid_size_y * (y + 0.5)
hotspots.append((lng, lat, count/2))
# 打印热点网格信息
for hotspot in hotspots:
print('经度:%.4f,纬度:%.4f,出租车数量:%d' % hotspot)
import matplotlib.pyplot as plt
# 绘制热点交互网络
fig, ax = plt.subplots()
for hotspot in hotspots:
lng = hotspot[0]
lat = hotspot[1]
count = hotspot[2]
circle = plt.Circle((lng, lat), 0.01, color='r', alpha=min(count/100, 1))
ax.add_artist(circle)
plt.xlim(lon1, lon2)
plt.ylim(lat1, lat2)
plt.show()
原文地址: https://www.cveoy.top/t/topic/n2Fu 著作权归作者所有。请勿转载和采集!