Plotting tracks#
Almost all core objects in tracklib have a graphical function for displaying data: plot.
There are different ways of displaying analytical tracks, as shown below.
You can create a set of plot in a same graphic with the append parameter: if the value is - True : append to the current plot - False: create a new plot - Ax : append to the fiven ax object
Importing Tracklib#
[1]:
import os
import sys
import matplotlib.pyplot as plt
# Import the Tracklib library
import tracklib as tkl
Code running in a no shapely environment
Loading a Sample GNSS Track#
This GNSS trajectory representes a running training session on an athletics track.
[2]:
tkl.ObsTime.setReadFormat("4Y-2M-2DT2h:2m:2sZ")
tracks = tkl.TrackReader.readFromFile('../../../data/gpx/activity_5807084803.gpx', tkl.TrackFormat({'ext':'GPX'}))
trace = tracks.getTrack(0)
# Transformation GEO coordinates to ENU
trace.toENUCoords()
#
trace.resample(50, tkl.MODE_SPATIAL)
Warning: no reference point (base) provided for local projection to ENU coordinates. Arbitrarily used: [lon= 2.457019882, lat=48.830705099, hgt= 55.200]
Basic Plots#
Several graphical elements can be displayed on the same figure using the append parameter.
When
append=False(default), a new figure is created.When
append=True, the graphical element is added to the current figure.Alternatively,
appendcan be set to an existing Matplotlib axis, in which case the element is drawn on that axis.
The examples below illustrate different ways of displaying a track:
Figure 1 displays the track as a line. The start point (
S) and the end point (E) are added to the same figure.Figure 2 displays the track as individual points. The
symparameter is set to">"to specify the marker used for each observation.Figure 3 displays the track in a circular coordinate system.
[3]:
plt.figure(figsize=(11, 2))
plt.subplots_adjust(top=1.3, wspace=0.2, hspace=0.5)
# ---------------------------------------------------------------------
#
ax1 = plt.subplot2grid((1, 3), (0, 0))
#trace.plot(type='LINE', append=ax1)
trace.plot(sym='g-', title='original extract track', append=ax1)
# plot first obs, plot last obs
trace.plotFirstObs(append=ax1)
trace.plotLastObs(append=ax1)
# ---------------------------------------------------------------------
#
ax2 = plt.subplot2grid((1, 3), (0, 1))
trace.plot(type='POINT', sym='g>', append=ax2)
# ax22.scatter(trace.getX(), trace.getY(), marker='o', c='#C0C0C0', s=5)
# ---------------------------------------------------------------------
#
ax3 = plt.subplot2grid((1, 3), (0, 2))
trace.plot(type='CIRCULAR', append=ax3)
plt.show()
Plot a track with markers#
The following plots illustrate several marker styles that can be used to display observations.
[4]:
plt.figure(figsize=(8, 5))
plt.subplots_adjust(top=1.3, wspace=0.2, hspace=0.2)
# Figure 1
ax1 = plt.subplot2grid((3, 3), (0, 0))
trace.plotAsMarkers()
# Figure 2
ax2 = plt.subplot2grid((3, 3), (0, 1))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_WARNING, append=ax2)
# Figure 3
ax3 = plt.subplot2grid((3, 3), (0, 2))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_NO_ENTRY, append=ax3)
# Figure 4
ax4 = plt.subplot2grid((3, 3), (1, 0))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_INTERDICTION, append=ax4)
# Figure 5
ax5 = plt.subplot2grid((3, 3), (1, 1))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_SPOT, append=ax5)
# Figure 6
ax6 = plt.subplot2grid((3, 3), (1, 2))
trace.plotAsMarkers(bkg='w', frg='c', sym_frg = " ", sym_bkg = "v", append=ax6)
# Figure 7
ax7 = plt.subplot2grid((3, 3), (2, 0))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_NO_STOP, size=8, append=ax7)
# Figure 8
ax8 = plt.subplot2grid((3, 3), (2, 1))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_INFORMATION, size=10, append=ax8)
# Figure 9
ax9 = plt.subplot2grid((3, 3), (2, 2))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_BOWTIE, size=10, append=ax9)
[4]:
<module 'matplotlib.pyplot' from '/home/md_vandamme/7_LIB/tracklibenv/tracklibenv/lib/python3.10/site-packages/matplotlib/pyplot.py'>
Plotting Analytical Features#
Tracklib provides several ways to visualize the values of an AnalyticalFeature (AF). The following examples demonstrate some of the available plots.
We first compute the speed analytical feature for the sample training track. The curvilinear abscissa is computed automatically as part of the same operation.
[5]:
# Speed computing
trace.estimate_speed()
print ('All AF: ', trace.getListAnalyticalFeatures())
print ('Speed for the first observation: ', trace['speed'][0])
All AF: ['abs_curv', 'speed']
Speed for the first observation: 5.533338627821578
Displaying Speed as a Raster#
[6]:
# Colors as a gradient
COLS = tkl.getColorMap((220, 220, 220), (255, 0, 0))
trace.plot(type='POINT', af_name='speed', append = False, cmap = COLS,
title='Speed values', w=4, h=3)
Displaying Speed and Elevation Profiles#
Speed and elevation can be plotted as profiles against either distance (curvilinear abscissa) or time.
[7]:
# -----------------------------------------------------------------------------------------------------
# PROFIL
# -----------------------------------------------------------------------------------------------------
# Sample
plt.figure(figsize=(12, 3))
plt.subplots_adjust(top=1.3, wspace=0.2, hspace=0.5)
trace.plotProfil('SPATIAL_SPEED_PROFIL', color="limegreen", linestyle='-', append=plt.subplot2grid((2, 2), (0, 0)))
trace.plotProfil('SPATIAL_ALTI_PROFIL', color="royalblue", linestyle='--', append=plt.subplot2grid((2, 2), (0, 1)))
trace.plotProfil('TEMPORAL_SPEED_PROFIL', color="lightseagreen", linestyle=':', append=plt.subplot2grid((2, 2), (1, 0)))
trace.plotProfil('TEMPORAL_ALTI_PROFIL', color="lightgreen", linestyle='-.', append=plt.subplot2grid((2, 2), (1, 1)))
Plotting Binary Analytical Features#
Binary AnalyticalFeatures can be superimposed on speed or elevation profiles to highlight observations matching a given condition.
[8]:
# With AF
# create two virtual transition AFs to visualize them
stops = [0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0]
trace.createAnalyticalFeature('STOP1', stops)
stops = [0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,1,0,0]
trace.createAnalyticalFeature('STOP2', stops)
TAB_AFS = ['STOP1', 'STOP2']
trace.plotProfil('SPATIAL_SPEED_PROFIL', TAB_AFS, append=False)
[9]:
plt.figure(figsize=(12, 2))
plt.subplots_adjust(top=1.3, wspace=0.2, hspace=0.2)
# -----------------------------------------------------------------------
# Boxplot
ax1 = plt.subplot2grid((1, 2), (0, 0))
trace.plotAnalyticalFeature('speed', 'BOXPLOT', append=ax1)
# Graphic values in Plot
ax2 = plt.subplot2grid((1, 2), (0, 1))
trace.plotAnalyticalFeature("speed", "PLOT", append=ax2)
Gallery Preview#
[10]:
plt.figure(figsize=(8, 4))
plt.subplots_adjust(top=1.3, wspace=0.5, hspace=0.5)
# Figure 1
ax1 = plt.subplot2grid((2, 2), (0, 0))
trace.plot(sym='g-', title='original extract track', append=ax1)
trace.plotFirstObs(append=ax1)
trace.plotLastObs(append=ax1)
# Figure 2
ax2 = plt.subplot2grid((2, 2), (0, 1))
trace.plotAsMarkers(type=tkl.MARKERS_TYPE_BOWTIE, size=10, append=ax2)
# Figure 3
ax3 = plt.subplot2grid((2, 2), (1, 0))
trace.plotProfil('TEMPORAL_SPEED_PROFIL', color="royalblue", linestyle='-.', append=ax3)
# Figure 4
ax4 = plt.subplot2grid((2, 2), (1, 1))
trace.plot(type='CIRCULAR', append=ax4)