Skip to content

Instantly share code, notes, and snippets.

@andyljones
Last active August 26, 2018 18:23
Show Gist options
  • Save andyljones/bb0e27a8f35fa746fa9e3103f017bd95 to your computer and use it in GitHub Desktop.
Save andyljones/bb0e27a8f35fa746fa9e3103f017bd95 to your computer and use it in GitHub Desktop.
Demonstrates the importance of perceptually uniform colormaps using temperature in Central England over the past 200 years
import requests
import pandas as pd
from io import BytesIO
import matplotlib as mpl
import matplotlib.pyplot as plt
url = 'https://www.metoffice.gov.uk/hadobs/hadcet/cetdl1772on.dat'
raw = pd.read_csv(BytesIO(requests.get(url).content), sep='\s+', header=None)
raw.columns = ['year', 'day'] + list(range(1, 13))
stacked = raw.set_index(['year', 'day']).stack().loc[lambda s: s != -999]/10
stacked.index = pd.to_datetime([f'{y:4d}-{m:02d}-{d:02d}' for y, d, m in stacked.index])
stacked = stacked.sort_index()
cmaps = ['gnuplot', 'inferno', 'viridis']
fig, axes = plt.subplots(len(cmaps), 1, sharex=True)
for cmap, ax in zip(cmaps, axes):
extent = (mpl.dates.date2num(stacked.index[0]), mpl.dates.date2num(stacked.index[-1]), 0, 1)
ax.imshow(stacked.resample('A').mean().values[None, :], extent=extent, aspect=2e4, cmap=cmap)
ax.set_yticks([])
ax.xaxis_date()
ax.set_title(cmap, size='medium')
ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment