pysplit
pysplit copied to clipboard
how can i plot the right graph?
hi Mellissa!
I want to plot a different color graph for each day.
So I made two codes.
The first one is
`import pysplit
trajgroup = pysplit.make_trajectorygroup(r'C:\Users\lg\Desktop\trajectory\jeju06*.txt')
with open(r"C:\Users\lg\Desktop\trajectory\jeju060100-500.txt"),open(r"C:\Users\lg\Desktop\trajectory\jeju060200-500.txt"),open(r"C:\Users\lg\Desktop\trajectory\jeju060300-500.txt") as traj: contents = traj.readlines()
color_dict = {1 : 'red', 2 : 'green', 3 : 'blue'}
for traj in trajgroup : last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = color_dict[day]mapcorners = [0, 00, 160, 80] standard_pm = None bmap_params = pysplit.MapDesign(mapcorners, standard_pm) bmap = bmap_params.make_basemap()
for traj in trajgroup[::1]: print(traj.filename) bmap.plot(*traj.path.xy, c=traj.trajcolor, latlon=True, zorder=20)`
_
And second one is
`import pysplit
trajgroup = pysplit.make_trajectorygroup(r'C:\Users\lg\Desktop\trajectory\jeju06*.txt')
with open(r"C:\Users\lg\Desktop\trajectory\jeju060100-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'red'with open(r"C:\Users\lg\Desktop\trajectory\jeju060200-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'orange'with open(r"C:\Users\lg\Desktop\trajectory\jeju060300-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'yellow'with open(r"C:\Users\lg\Desktop\trajectory\jeju060400-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'green'with open(r"C:\Users\lg\Desktop\trajectory\jeju060500-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju060600-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'black'with open(r"C:\Users\lg\Desktop\trajectory\jeju060700-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'purple'with open(r"C:\Users\lg\Desktop\trajectory\jeju060800-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju060900-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju061000-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju061100-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju061200-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju061300-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'with open(r"C:\Users\lg\Desktop\trajectory\jeju061400-500.txt") as traj: contents = traj.readlines()
last_timepoint = 9 data = contents[last_timepoint].split()
day = int(data[4]) traj.trajcolor = 'blue'mapcorners = [0, 00, 160, 80] standard_pm = None bmap_params = pysplit.MapDesign(mapcorners, standard_pm) bmap = bmap_params.make_basemap()
for traj in trajgroup: print(traj.filename) bmap.plot(*traj.path.xy, c=traj.trajcolor, latlon=True, zorder=20) `
then Its results are shown in the following figures.
How can I make the right graph?
Best Regards Chaelin.
Hi Chaelin,
The trajectories loaded in the with open()
statements have no connection to the Trajectory objects initiated in trajgroup
, despite being derived from the same files. This is why in the second example none of the colors are applied. In the first example, you get all blue trajectories because the third file is opened, contents
is created and persists, and trajectory color is assigned to all Trajectory objects in trajgroup
based on these contents
from that one trajectory.
What you need to do is inspect the Trajectory objects in your TrajectoryGroup and assign colors to them, NOT reopen and parse the original files. To make your second example work:
import pysplit
import pandas as pd
trajgroup = pysplit.make_trajectorygroup(r'C:\Users\lg\Desktop\trajectory\jeju06*.txt')
color_dict = {1 : 'red', 2 : 'orange', 3 : 'yellow', 4 :'green', 6 : 'purple', 7 : 'black'}
for traj in trajgroup:
try:
traj.trajcolor = color_dict[pd.to_datetime(traj.data.DateTime[0]).day]
except KeyError:
traj.trajcolor = 'blue'
And then plot. The important bit here is that you can get the start day of a Trajectory object via pd.to_datetime(traj.data.DateTime[0])
.
Hope that helps!