Skip to content

Instantly share code, notes, and snippets.

@barronh
Last active August 31, 2023 13:51
Show Gist options
  • Save barronh/093e21501f6cf94265372ac92dacd726 to your computer and use it in GitHub Desktop.
Save barronh/093e21501f6cf94265372ac92dacd726 to your computer and use it in GitHub Desktop.
Matplotlib EPA AQI cmap
import matplotlib.pyplot as plt
import matplotlib.colors as mc
import numpy as np
pmedges = np.asarray([0.0, 12.1, 35.5, 55.5, 150.5, 250.5, 500.5])
o8edges = np.asarray([0.0, 55, 71, 86, 106, 201, 500.])
o1edges = np.asarray([0.0, 60, 125,165,205, 405, 604.])
aqicolors = ['#00e400', '#ffff00', '#ff7e00', '#ff0000', '#8f3f97', '#7e0023']
colors = ['grey'] + aqicolors + ['k']
pmcmap, pmnorm = mc.from_levels_and_colors(pmedges, colors, extend='both')
o8cmap, o8norm = mc.from_levels_and_colors(o8edges, colors, extend='both')
testz = np.array([
[-1e6, -1e3, -1],
[0, 6, 12],
[12.1, 25, 35.4],
[35.5, 40, 55.4],
[55.5, 100, 150.4],
[150.5, 200, 250.4],
[250.5, 350, 500.4],
[500.5, 1e3, 1e5],
])
qm = plt.pcolormesh(testz, cmap=pmcmap, norm=pmnorm)
cbar = plt.colorbar(qm, ticks=(pmedges[:-1] + pmedges[1:])/2, orientation='horizontal')
cbar.ax.set_xticklabels(['Good', 'Moderate', 'USG', 'Unhealthy', 'Very U', 'Hazardous'])
@barronh
Copy link
Author

barronh commented Aug 31, 2023

The code above produces the figure below. Note that there three columns of identical color, which makes the rows look like stripes.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment