【问题标题】:Python Pandas: Ploting GPS track as it evolves in timePython Pandas:随着时间的推移绘制 GPS 轨迹
【发布时间】:2018-01-04 17:53:17
【问题描述】:

我有一个数据框,其中包含按日期时间索引的纬度、经度列。时间序列数据表示对象在时间上的轨迹:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 137824 entries, 2015-03-01 07:16:39 to 2015-04-01 00:12:38
Data columns (total 5 columns):
accuracy     137824 non-null float64
longitude    137824 non-null float64
latitude     137824 non-null float64

如何从这些点绘制轨迹以查看它如何随时间变化?我首先使用 Shapely 创建数据框中所有点的有序列表:

import geopandas as gpd
from shapely.geometry import Point

geometry = [Point(xy) for xy in zip(df.longitude, df.latitude)]

# Define EPSG4326 coordinate reference system for GPS latitude and longitude (CRS)
crs = {'init': 'epsg:4326'}
points = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)

然后我可以绘制所有点:

points.plot(color='green')

这会绘制 2D 空间中 x=纬度和 y=经度的所有点。如何按时间顺序(3D)绘制这些点?

* 更新 *

毕竟,用 Geopandas 绘图并不是我的最终目标。我正在寻找任何可以及时绘制 GPS 坐标的方法。也许使用常规的 Pandas 绘图方法或 seaborn 绘图库。这个想法是可视化移动对象坐标如何随时间变化,也就是说显示其位置的历史。有什么想法吗?

【问题讨论】:

标签: python pandas gps time-series


【解决方案1】:

我不知道这是否是你需要的,但我想我可以使用 Matplotlib 库。

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.dates as mdates

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('Time')
ax.plot(df2['lon'].values, df2['lat'].values, mdates.date2num(df2['dtime'].tolist()),label=str(1))
ax.zaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M:%S'))
ax.legend()
plt.show()

说明:

label的作用是识别当前数据属于哪个类别,我以ID为例。

mdates.date2numset_major_formatter结合使用。 如果我们使用如下代码:

ax.plot(df2['lon'].values, df2['lat'].values, df2['dtime'].values,label=str(1))

结果,图时间轴显示为大整数:

如果我们只使用set_major_formatter来修改时间显示格式会出错:

OverflowError: Python int too large to convert to C long

mdates.date2num将时间转换成更小的数字,所以我们需要使用mdates.date2numset_major_formatter

【讨论】:

  • 是的,我需要这样的东西,谢谢!此代码有效,但仅沿所有三个轴显示我的数据的一部分:x、y 和 z。如何调整绘图大小以显示所有数据?我也收到此警告:“.../usr/anaconda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:531:用户警告:未找到标记的对象。使用标签='...'个别地块上的 kwarg。warnings.warn("未找到标记的对象。""
  • 警告缺少标签。如果有多个track,可以用它来标注ID,可以看更新的代码和图片。我不明白显示所有数据意味着什么。
  • 你的意思是时间没有完全错?这是我纠正的错误。
  • 我得到 KeyError: 'taxi_id' 什么是'tax_id'?我的数据中没有这样的列。请同时解释“mdates.date2num(df2['date'].tolist()),label=str(i)”的用途。它应该做什么?
  • 对不起,我以ID为例,因为我认为你有很多跟踪,我已经更新了内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多