Created
March 30, 2021 22:51
-
-
Save joshdorrington/e7fb19c88a097bec21a86de01d5299ba to your computer and use it in GitHub Desktop.
This is how I made my cherry_blossom plot
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 numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.offsetbox import OffsetImage, AnnotationBbox | |
from matplotlib.cbook import get_sample_data | |
def imscatter(x, y, image, ax=None, zoom=1,alpha=1): | |
if ax is None: | |
ax = plt.gca() | |
try: | |
image = plt.imread(image) | |
except TypeError: | |
# Likely already an array... | |
pass | |
im = OffsetImage(image, zoom=zoom,alpha=alpha) | |
x, y = np.atleast_1d(x, y) | |
artists = [] | |
for x0, y0 in zip(x, y): | |
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False) | |
artists.append(ax.add_artist(ab)) | |
ax.update_datalim(np.column_stack([x, y])) | |
ax.autoscale() | |
return artists | |
image_path = get_sample_data(blossom_pic_path) | |
year,flwr_day,flwr_date,mode,_,__=np.genfromtxt(data_txt_file,delimiter=",").T | |
#Get the smoothed series | |
smoothing=100 | |
ys=np.arange(np.nanmin(year),np.nanmax(year)+1) | |
smooth_flwr_day=np.array([np.nanmean(flwr_day[np.isin(year,ys[t:t+smoothing])]) for\ | |
t in np.arange(len(ys)-smoothing)]) | |
ys=ys[smoothing//2:-smoothing//2] | |
#Make the plot | |
fig, ax = plt.subplots() | |
fig.set_figwidth(18) | |
fig.set_figheight(6) | |
ax.set_facecolor("xkcd:light blue") | |
imscatter(year, flwr_day, image_path, zoom=0.01, ax=ax,alpha=0.7) | |
#add smoothed line | |
ax.plot(ys,smooth_flwr_day,c="saddlebrown",lw=3) | |
#Add the minima line | |
preindust_ys=ys[np.argwhere(ys<1850)] | |
preindust_minday=smooth_flwr_day[np.isin(ys,preindust_ys)].min() | |
x0,x1=ax.get_xlim() | |
ax.hlines(preindust_minday,x0,x1,linestyles=":",colors="gray") | |
ax.set_xlim(x0,x1) | |
#prettifying. | |
ax.set_yticks([91,101,111,121]) | |
ax.set_yticklabels(["01/04","11/04","21/04","01/05"],fontname="candara")#Assumes non leap year | |
ax.set_xticks(np.arange(800,2100,100)) | |
ax.set_xticklabels(np.arange(800,2100,100),fontname="candara") | |
ax.set_ylabel("Day of the year\n cherry blossoms\n fully flowered\n in Kyoto",\ | |
fontsize=18,rotation=0,fontname="candara",labelpad=70) | |
ax.set_xlabel("Year",fontsize=18,fontname="candara") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment