简体   繁体   中英

Python plot trajectories density

I am working on trajectories and I would like to plot a "density map" of the trajectories.

Here is a sample of a code with only 4 trajectories which I can use to plot trajectories on a base-map for example.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

map = Basemap(projection="merc",resolution="i",llcrnrlat=25,urcrnrlat=65,llcrnrlon=-100,urcrnrlon=-55) 
map.bluemarble()

dico={}
dico["traj1"]=[[27.5, -86.5], [35.5, -85.0], [40.5, -84.0], [45.5, -82.5], [46.0, -81.5], [47.5, -79.5], [48.5, -78.0], [49.5, -76.0], [51.0, -75.0]]
dico["traj2"]=[[27.5, -88.5], [35.5, -92.5], [40.5, -97.5], [45.5, -93.5], [48.5, -90.0], [53.5, -95.0], [57.0, -100.0], [55.5, -102.0], [53.5, -101.5]]
dico["traj3"]=[[27.5, -88.5], [33.5, -88.0], [34.0, -86.5], [34.0, -84.5], [34.0, -82.0], [34.0, -80.0], [34.0, -78.5], [34.0, -77.5], [32.5, -77.0]]
dico["traj4"]=[[27.5, -86.5], [35.5, -82.5], [40.5, -77.5], [45.5, -78.5], [47.5, -77.5], [47.0, -76.0], [46.0, -73.0], [45.5, -70.5], [45.5, -66.5]]

for i in range(0,4) :
    lons_values=[item[1] for item in dico["traj"+str(i+1)]]
    lats_values=[item[0] for item in dico["traj"+str(i+1)]]

    z,t = map(lons_values, lats_values)
    map.plot(z, t, linewidth=3.5,color=np.random.rand(3,1))

Does anyone know how to draw an envelop with contours describing the main pattern of the trajectories? For example if I have 100 trajectories I would like to visualize different contours for which the color indicates the "concentration" of trajectories in this path.

If we consider that every point of a trajectory corresponds to one time step, if an information about the time step (like contour lines crossing each point of time step t) could also be present on the map, it would be great (so that information about time and space are conserved).

I found a solution to my question using the seaborn module:

I used sns.kdeplot(df['x'],df['y'], shade=True) to obtain the plot. Then I just had to project it on the map.

You can find the tutorial I used by following this link:

Seaborn density plot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM