Skip to content

Instantly share code, notes, and snippets.

@hbshrestha
Created October 3, 2022 19:57
Show Gist options
  • Save hbshrestha/db1cd9b61047471e934a2f6e8da252cf to your computer and use it in GitHub Desktop.
Save hbshrestha/db1cd9b61047471e934a2f6e8da252cf to your computer and use it in GitHub Desktop.
code to create warming stripes
anomaly = df["Annual Mean"]
fig = plt.figure(figsize = (10, 1.5))
ax = fig.add_axes([0, 0, 1, 1])
#turn the x and y-axis off
ax.set_axis_off()
#create a collection with a rectangle for each year
col = PatchCollection([
Rectangle((y, -1), #xy
1, #width
2 #height
) for y in np.arange(1880, 2022)])
#use the anomaly data for colormap
col.set_array(anomaly)
#apply the colormap colors
cmap = plt.get_cmap("coolwarm")
col.set_cmap(cmap)
ax.add_collection(col)
#average global temperature line
df.plot(ax = ax, linestyle = "-", color = "white", legend = False)
ax.axhline(0, linestyle = "--", color = "white")
#add title and text
ax.set_title("Warming Stripes (1880-2021)", loc = "center", y = 0.75)
ax.text(x = 1880, y = -1, s = "1880")
ax.text(x = 2022, y = -1, s = "2021")
ax.set_ylim(-1, 2)
ax.set_xlim(1870, 2030)
fig.savefig("../output/warming_stripes.jpeg",dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment