You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pandas在Python中统计并绘制经纬度访问点随时间的柱状图

没问题,我来帮你一步步搞定这两个需求!咱们从准备工作开始,然后分别完成可视化任务:

准备工作

首先得导入需要的Python库,然后读取并预处理你的CSV数据——注意你的CSV用的是|作为分隔符,而且列名和数据里有多余空格,得先处理干净:

import pandas as pd
import matplotlib.pyplot as plt

# 读取CSV,指定分隔符为|,并跳过字段前的空格
df = pd.read_csv('your_file.csv', sep='|', skipinitialspace=True)

# 清理列名(去掉前后空格)
df.columns = df.columns.str.strip()

# 可选:将date和time合并为完整的datetime列,方便后续时间相关操作
df['datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'], format='%B %Y %H:%M:%S')
1. 绘制经纬度随时间变化的柱状图

因为要展示每个时间点的经纬度值,咱们可以用两个子图分别展示纬度和经度的变化,x轴用合并后的datetime列(或者单独的time列),y轴对应经纬度数值:

# 创建画布和两个子图
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))

# 绘制纬度随时间的柱状图
ax1.bar(df['datetime'], df['latitude'], color='skyblue')
ax1.set_title('Latitude Over Time')
ax1.set_ylabel('Latitude')
ax1.tick_params(axis='x', rotation=45)

# 绘制经度随时间的柱状图
ax2.bar(df['datetime'], df['longitude'], color='salmon')
ax2.set_title('Longitude Over Time')
ax2.set_ylabel('Longitude')
ax2.set_xlabel('Datetime')
ax2.tick_params(axis='x', rotation=45)

# 调整子图间距
plt.tight_layout()
plt.show()

如果不想合并datetime,直接用time列作为x轴也可以,把代码里的df['datetime']换成df['time']就行。

2. 统计不同时间/日期的访问点数量并绘制柱状图

这个需求分两种场景:按日期统计,或者按具体时间统计,咱们分别来实现:

按日期统计访问点数量

# 按日期分组统计数量
date_counts = df['date'].value_counts().sort_index()

# 绘制柱状图
plt.figure(figsize=(8, 5))
date_counts.plot(kind='bar', color='lightgreen')
plt.title('Number of Access Points by Date')
plt.xlabel('Date')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.show()

按具体时间统计访问点数量

# 按时间分组统计数量
time_counts = df['time'].value_counts().sort_index()

# 绘制柱状图
plt.figure(figsize=(8, 5))
time_counts.plot(kind='bar', color='orange')
plt.title('Number of Access Points by Time')
plt.xlabel('Time')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.show()

如果你的数据里有多个日期+时间的组合,也可以用合并后的datetime列来统计,把代码里的df['date']或df['time']换成df['datetime']即可。

内容的提问来源于stack exchange,提问作者Rigzin Angmo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:04:47