代码之家  ›  专栏  ›  技术社区  ›  dokondr

巨蟒熊猫:随着时间的推移绘制GPS轨迹

  •  0
  • dokondr  · 技术社区  · 7 年前

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

    <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坐标的方法。可能使用常规熊猫绘图方法或seaborn绘图库。其想法是可视化移动对象坐标如何随时间变化,也就是说显示其位置的历史。有什么想法吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   giser_yugang    7 年前

    我不知道这是否是您需要的,但我想我可以使用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()
    

    enter image description here

    解释:

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

    mdates.date2num 与一起使用 set_major_formatter . 如果我们使用以下代码

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

    因此,图形时间轴显示为大整数 enter image description here

    如果我们只使用 set\u major\u格式化程序 修改时间显示格式以获取错误的

    OverflowError: Python int too large to convert to C long
    

    日期。date2num日期 将时间转换为较小的数字,因此我们需要使用 日期。date2num日期 set\u major\u格式化程序