Skip to content

Commit

Permalink
add 2023 uk nye plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Nosudrum committed Sep 14, 2024
1 parent 1b967a0 commit aa40a30
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ I started tracking all my journeys by train in a spreadsheet in 2022, and manage
<a href="https://raw.githubusercontent.com/Nosudrum/train-stats/main/plots/2019_china_portrait.png">
<img src="plots/2019_china_portrait.png" width="49%" />
</a>
<a href="https://raw.githubusercontent.com/Nosudrum/train-stats/main/plots/2023_uk_nye.png">
<img src="plots/2023_uk_nye.png" width="49%" />
</a>
</p>
Binary file added plots/2023_uk_nye.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from plotsCodes.october_lausanne_2024 import plot_october_lausanne_2024
from plotsCodes.scandinavia_2024 import plot_scandinavia_2024
from plotsCodes.scandinavia_2024_portrait import plot_scandinavia_2024_portrait
from plotsCodes.uk_nye_2023 import plot_uk_nye_2023
from processing import process_data
from utils import get_mapbox_secrets

Expand All @@ -29,6 +30,8 @@
plot_scandinavia_2024_portrait(trips, mapbox_style_id=MAPBOX_STYLE_ID, mapbox_style_token=MAPBOX_STYLE_TOKEN)
plot_milano_iac_2024(trips, mapbox_style_id=MAPBOX_STYLE_ID, mapbox_style_token=MAPBOX_STYLE_TOKEN)
plot_october_lausanne_2024(trips, mapbox_style_id=MAPBOX_STYLE_ID, mapbox_style_token=MAPBOX_STYLE_TOKEN)
plot_uk_nye_2023(trips, mapbox_style_id=MAPBOX_STYLE_ID, mapbox_style_token=MAPBOX_STYLE_TOKEN)

plot_number_per_duration_stacked(trips)
plot_distance_per_duration_stacked(trips)

Expand Down
106 changes: 106 additions & 0 deletions src/plotsCodes/uk_nye_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import json
from datetime import datetime

import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
from cartopy.crs import Robinson, PlateCarree
from cartopy.io.img_tiles import MapboxStyleTiles
from matplotlib.colors import LinearSegmentedColormap
from tqdm import tqdm

from utils import dark_figure, finish_map, JOURNEYS_PATH, extract_trips_journeys, compute_stats, PARIS_TZ, \
get_trip_labels

# Trip start/end dates
START = datetime(2023, 12, 22, 0, 0, 0, tzinfo=PARIS_TZ)
END = datetime(2024, 1, 1, 23, 59, 59, tzinfo=PARIS_TZ)

# Setup map boundaries
LON_MIN = -3
LON_MAX = 8.5
LAT_MIN = 41
LAT_MAX = 54
ZOOM_LEVEL = 6


def plot_uk_nye_2023(trips, mapbox_style_token, mapbox_style_id):
# Compute trips and journeys dataframe
trips, journeys = extract_trips_journeys(trips, filter_start=START, filter_end=END)

# Setup figure
fig, ax = dark_figure(grid=False, projection=Robinson(), figsize=(4, 7.1111))
request = MapboxStyleTiles(mapbox_style_token, "nosu", mapbox_style_id, cache=False)
extent = [LON_MIN, LON_MAX, LAT_MIN, LAT_MAX]
ax[0].set_extent(extent)
ax[0].add_image(request, ZOOM_LEVEL, regrid_shape=3000)

# For all journeys in the dataset
now = datetime.now(tz=PARIS_TZ)
trip_duration_days = (END - START).days + 1
color_map = matplotlib.colormaps["rainbow"]

trip_list = trips.index.to_list()
for trip in tqdm(trip_list, ncols=150, desc="UK NYE 2023 (portrait)"):
# Get the departure datetime
trip_departure = trips.loc[trip, "Departure (Local)"]
# Compute the trip day number
trip_day = (trip_departure.date() - START.date()).days + 1
# Color corresponding to the trip day
trip_ratio = (trip_day - 1) / (trip_duration_days - 1) # value must be between 0 and 1
c = color_map(trip_ratio)

# Dashes or not
if trips.loc[trip, "Arrival (Local)"] < now:
s = "-"
dashes = [1, 0]
else:
s = ":"
dashes = [1, 1 + trip_ratio]

# Plot the trip
with open(JOURNEYS_PATH + trips.loc[trip, "journey"] + ".geojson", 'r', encoding="utf8") as f:
geojson = json.load(f)
coords = np.array(geojson['features'][0]['geometry']['coordinates'])
ax[0].plot(coords[:, 0], coords[:, 1], s, linewidth=1.2, color=c, transform=PlateCarree(),
zorder=(trip_duration_days - trip_day) + 2,
solid_capstyle="round",
dashes=dashes
)

# Logging
print("Finalizing plot...")

# Setup colorbar
color_map_list = np.array([color_map(i) for i in range(color_map.N)])
custom_map = LinearSegmentedColormap.from_list(
'Custom cmap', color_map_list.tolist(), color_map.N)
bounds = np.linspace(0, trip_duration_days + 2, trip_duration_days + 1)
sm = cm.ScalarMappable(cmap=custom_map, norm=plt.Normalize(vmin=0, vmax=trip_duration_days + 1))
sm.set_array([])
cax = ax[0].inset_axes([0.08, 0.08, 0.8, 0.035])
cbar = fig.colorbar(sm, orientation="horizontal", cax=cax, boundaries=bounds,
ticks=np.linspace(0.5, trip_duration_days + 1.5, trip_duration_days))
cbar_title, cbar_ticks = get_trip_labels(START, END)
cbar.ax.set_title(cbar_title, color="white", fontsize=8)
cbar.ax.set_xticklabels(cbar_ticks)
cbar.outline.set_edgecolor('white')
plt.setp(plt.getp(cbar.ax, 'xticklabels'), color='white', fontsize=12)
plt.tight_layout()

# Stats
distance_str, duration_str = compute_stats(trips, start=START, end=END, timezone=PARIS_TZ)
fig_axes = fig.add_axes([0.95, 0.027, 0.3, 0.3], anchor="SE", zorder=1)
index = duration_str.find(" out of")
if index != -1:
# Split the duration in two lines
fig_axes.text(0, 0.08, duration_str[:index], ha="right", va="bottom", color="white", fontsize=9)
fig_axes.text(0, 0, duration_str[index:], ha="right", va="bottom", color="white", fontsize=9)
fig_axes.text(0, 0.19, distance_str, ha="right", va="bottom", color="white", fontsize=9)
else:
fig_axes.text(0, 0.03, duration_str, ha="right", va="bottom", color="white", fontsize=9)
fig_axes.text(0, 0.16, distance_str, ha="right", va="bottom", color="white", fontsize=9)
fig_axes.axis("off")

finish_map(fig, ax, "2023_uk_nye", colorbar=cbar, show=False, logo_position=[0.05, 0.656, 0.4, 0.3])

0 comments on commit aa40a30

Please sign in to comment.