Created
October 30, 2024 00:18
-
-
Save mattharrison/cc4a70572853ffb93ec694ac1a34099f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
url = 'http://atmenv.envi.osakafu-u.ac.jp/omu-content/uploads/sites/1215/2015/10/KyotoFullFlower7.xls' | |
cherry_raw = pd.read_excel(url, skiprows=25, dtype_backend='pyarrow') | |
def tweak_cherry(df): | |
return (df | |
#.query('~`Full-flowering date (DOY)`.isna()') | |
.rename(columns={'AD': 'year', | |
'Full-flowering date (DOY)': 'flowering_doy', | |
'Full-flowering date': 'flowering_MDD'}) | |
.assign( | |
month=lambda df_: df_['flowering_MDD']//100, | |
day=lambda df_: df_['flowering_MDD'] - (df_['month'] *100), | |
#date=lambda df_: pd.to_datetime(df_[['year', 'month', 'day']], format='%Y%m%d', errors='coerce') | |
) | |
#.drop(columns=['month', 'day']) | |
#.dropna(subset=['date']) | |
#.assign(rolling_doy=lambda x: x['flowering_doy'].rolling(window=20).mean()) | |
#.loc[:, ['year', 'date', 'flowering_doy', 'rolling_doy', 'flowering_MDD']] | |
) | |
def plot_cherry(df): | |
plt.rcParams["font.family"] = "Roboto" | |
figsize = (160, 165) # pts | |
def points_to_inches(points): | |
return points / 72 | |
figsize_inches = [points_to_inches(dim) for dim in figsize] | |
padding = 6 # pts | |
heading_fontsize = 11 | |
heading_fontweight = 'bold' | |
subheading_fontsize = 9.5 | |
subheading_fontweight = 'normal' | |
source_fontsize = 6.5 | |
source_fontweight = 'light' | |
axis_fontsize = 7.5 | |
axis_fontweight = 'normal' | |
tick_fontsize = 7 | |
tick_fontweight = 'light' | |
fig, ax = plt.subplots(figsize=figsize_inches, dpi=300) | |
df.plot.scatter(x='year', y='flowering_doy', ax=ax, c='#e7afbe', | |
s=3.5) | |
df.plot(x='year', y='rolling', ax=ax, c='#bd4758', linewidth=1.5) | |
# add label color of line to end of line | |
ax.text(df['year'].iloc[-1], df['rolling'].iloc[-1], '20-year\n average', color='#bd4758', | |
fontsize=tick_fontsize, fontweight='normal') | |
# set title | |
plt.figtext(0.0 , 1, 'Cherry trees have been blossoming earlier\ndue to warmer spring temperatures', ha='left', | |
fontsize=heading_fontsize, fontweight=heading_fontweight) | |
# set subtitle | |
plt.figtext(0, 0.93, 'Date of peak cherry tree (Prunus jamasakura) blossom in Kyoto, Japan', | |
fontsize=subheading_fontsize, fontweight=subheading_fontweight) | |
# get rid of the spines | |
for spine in ax.spines.values(): | |
spine.set_visible(False) | |
# get rid of y-axis ticks | |
ax.tick_params(axis='y', which='both', left=False) | |
# set font size and weight for x-axis labels | |
for label in ax.get_xticklabels(): | |
label.set_fontsize(tick_fontsize) | |
label.set_fontweight(tick_fontweight) | |
for label in ax.get_yticklabels(): | |
label.set_fontsize(tick_fontsize) | |
label.set_fontweight(tick_fontweight) | |
# set ticks at location/labels | |
# [(70, 'March 11'), (80, 'March 21'), (90, 'March 31'), (100, 'April 10'), (110, 'April 20'), (120, 'April 30')], | |
ax.set_yticks([70, 80, 90, 100, 110, 120]) | |
ax.set_yticklabels(['March 11', 'March 21', 'March 31', 'April 10', 'April 20', 'April 30']) | |
# add dashed grid to y-axis | |
ax.grid(axis='y', linestyle='--', alpha=0.7) | |
# set x ticks to 812, 1000, 1200, 1400, 1600, 1800, 2023 | |
ax.set_xticks([812, 1000, 1200, 1400, 1600, 1800, 2023]) | |
# get rid of x and y axis labels | |
ax.set_xlabel('') | |
ax.set_ylabel('') | |
# add source text at the bottom | |
plt.figtext(0.00, -0.10, 'Source: Yasuki Aono (2021; 2024)', ha='left', | |
fontsize=source_fontsize, fontweight=source_fontweight, | |
color='gray') | |
# hide legend | |
ax.legend().set_visible(False) | |
plt.show() | |
(tweak_cherry(cherry_raw) | |
.assign(rolling=lambda df_: df_['flowering_doy'].interpolate().rolling(window=20).mean()) | |
.pipe(plot_cherry) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment