11import pandas as pd
22import matplotlib .pyplot as plt
33
4+
45def clean_mta_df (df : pd .DataFrame ) -> pd .DataFrame :
56 out = df .copy ()
67
@@ -12,42 +13,84 @@ def clean_mta_df(df: pd.DataFrame) -> pd.DataFrame:
1213
1314 return out
1415
16+
1517def plot_ridership_recovery (df : pd .DataFrame ) -> plt .Figure :
1618 """Plot MTA ridership recovery by transit mode as % of pre-pandemic levels."""
1719 required_cols = [
1820 "date" ,
19- "subways_of_comparable_pre_pandemic_day " ,
20- "buses_of_comparable_pre_pandemic_day " ,
21- "lirr_of_comparable_pre_pandemic_day " ,
22- "metro_north_of_comparable_pre_pandemic_day " ,
21+ "subways_pct_of_comparable_pre_pandemic_day " ,
22+ "buses_pct_of_comparable_pre_pandemic_day " ,
23+ "lirr_pct_of_comparable_pre_pandemic_day " ,
24+ "metro_north_pct_of_comparable_pre_pandemic_day " ,
2325 ]
26+
2427 missing = [c for c in required_cols if c not in df .columns ]
2528 if missing :
2629 raise KeyError (f"Missing required columns: { missing } " )
2730
31+ plot_df = df .copy ()
32+ plot_df ["subways_pct_of_comparable_pre_pandemic_day" ] = (
33+ plot_df ["subways_pct_of_comparable_pre_pandemic_day" ] * 100
34+ )
35+ plot_df ["buses_pct_of_comparable_pre_pandemic_day" ] = (
36+ plot_df ["buses_pct_of_comparable_pre_pandemic_day" ] * 100
37+ )
38+ plot_df ["lirr_pct_of_comparable_pre_pandemic_day" ] = (
39+ plot_df ["lirr_pct_of_comparable_pre_pandemic_day" ] * 100
40+ )
41+ plot_df ["metro_north_pct_of_comparable_pre_pandemic_day" ] = (
42+ plot_df ["metro_north_pct_of_comparable_pre_pandemic_day" ] * 100
43+ )
44+
2845 fig , ax = plt .subplots (figsize = (14 , 7 ))
2946
30- ax .plot (df ["date" ], df ["subways_of_comparable_pre_pandemic_day" ],
31- label = "Subway" , alpha = 0.8 , linewidth = 1.2 )
32- ax .plot (df ["date" ], df ["buses_of_comparable_pre_pandemic_day" ],
33- label = "Bus" , alpha = 0.8 , linewidth = 1.2 )
34- ax .plot (df ["date" ], df ["lirr_of_comparable_pre_pandemic_day" ],
35- label = "LIRR" , alpha = 0.8 , linewidth = 1.2 )
36- ax .plot (df ["date" ], df ["metro_north_of_comparable_pre_pandemic_day" ],
37- label = "Metro-North" , alpha = 0.8 , linewidth = 1.2 )
47+ ax .plot (
48+ plot_df ["date" ],
49+ plot_df ["subways_pct_of_comparable_pre_pandemic_day" ],
50+ label = "Subway" ,
51+ alpha = 0.8 ,
52+ linewidth = 1.2 ,
53+ )
54+ ax .plot (
55+ plot_df ["date" ],
56+ plot_df ["buses_pct_of_comparable_pre_pandemic_day" ],
57+ label = "Bus" ,
58+ alpha = 0.8 ,
59+ linewidth = 1.2 ,
60+ )
61+ ax .plot (
62+ plot_df ["date" ],
63+ plot_df ["lirr_pct_of_comparable_pre_pandemic_day" ],
64+ label = "LIRR" ,
65+ alpha = 0.8 ,
66+ linewidth = 1.2 ,
67+ )
68+ ax .plot (
69+ plot_df ["date" ],
70+ plot_df ["metro_north_pct_of_comparable_pre_pandemic_day" ],
71+ label = "Metro-North" ,
72+ alpha = 0.8 ,
73+ linewidth = 1.2 ,
74+ )
3875
39- ax .axhline (y = 1.0 , color = "gray" , linestyle = "--" , linewidth = 1.5 ,
40- label = "Pre-pandemic baseline (100%)" )
76+ ax .axhline (
77+ y = 100 ,
78+ color = "gray" ,
79+ linestyle = "--" ,
80+ linewidth = 1.5 ,
81+ label = "Pre-pandemic baseline (100%)" ,
82+ )
4183
4284 ax .set_xlabel ("Date" , fontsize = 12 )
4385 ax .set_ylabel ("% of Pre-Pandemic Ridership" , fontsize = 12 )
4486 ax .set_title (
4587 "MTA Ridership Recovery: Subway vs Bus vs Commuter Rail (2020-Present)" ,
46- fontsize = 14 , fontweight = "bold" ,
88+ fontsize = 14 ,
89+ fontweight = "bold" ,
4790 )
4891 ax .legend (loc = "lower right" , fontsize = 10 )
4992 ax .grid (True , alpha = 0.3 )
50- ax .set_ylim (0 , 1.5 )
93+ ax .set_ylim (0 , 150 )
5194 fig .tight_layout ()
5295
53- return fig
96+ return fig
0 commit comments