Python Pandas:随着时间的推移绘制 GPS 轨迹
Python Pandas: Ploting GPS track as it evolves in time
我有一个数据框,其中包含按日期时间索引的纬度、经度列。时间序列数据表示一个对象在时间上的轨迹:
<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')
这会绘制二维空间中的所有点 space,其中 x=纬度,y=经度。如何按时间顺序绘制这些点(3D)?
*更新*
用 Geopandas 绘图毕竟不是我的最终目标。我正在寻找任何可以及时绘制 GPS 坐标的方法。也许使用常规 Pandas 绘图方法或 seaborn 绘图库。这个想法是可视化移动物体坐标如何随时间变化,也就是说显示其位置的历史。有什么想法吗?
我不知道这是否是你需要的,但我想我可以使用 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.date2num
与set_major_formatter
结合使用。
如果我们使用下面的代码:
ax.plot(df2['lon'].values, df2['lat'].values, df2['dtime'].values,label=str(1))
结果图表时间轴显示为一个大int:
如果我们只用set_major_formatter
修改时间显示格式会出错:
OverflowError: Python int too large to convert to C long
mdates.date2num
将时间转换成更小的数字,所以我们需要使用mdates.date2num
和set_major_formatter
我有一个数据框,其中包含按日期时间索引的纬度、经度列。时间序列数据表示一个对象在时间上的轨迹:
<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')
这会绘制二维空间中的所有点 space,其中 x=纬度,y=经度。如何按时间顺序绘制这些点(3D)?
*更新*
用 Geopandas 绘图毕竟不是我的最终目标。我正在寻找任何可以及时绘制 GPS 坐标的方法。也许使用常规 Pandas 绘图方法或 seaborn 绘图库。这个想法是可视化移动物体坐标如何随时间变化,也就是说显示其位置的历史。有什么想法吗?
我不知道这是否是你需要的,但我想我可以使用 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.date2num
与set_major_formatter
结合使用。
如果我们使用下面的代码:
ax.plot(df2['lon'].values, df2['lat'].values, df2['dtime'].values,label=str(1))
结果图表时间轴显示为一个大int:
如果我们只用set_major_formatter
修改时间显示格式会出错:
OverflowError: Python int too large to convert to C long
mdates.date2num
将时间转换成更小的数字,所以我们需要使用mdates.date2num
和set_major_formatter