HTML version of this notebook with outputs: https://statistikstadtzuerich.github.io/ssz-palette/example.html

SSZ Palette - colormaps in matplotlib

SSZ Palette provides commonly used colormaps of Statistik Stadt Zürich (SSZ) for matplotlib.

Import and register colormaps

In [1]:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

import sszpalette
In [2]:
# register the ssz color palette
colorsmaps = sszpalette.register()
colorsmaps
Out[2]:
['gray10',
 'gray10cool',
 'gray10warm',
 'harmonic6',
 'harmonic12',
 'harmonic6hell',
 'contrasting12',
 'contrasting12hell',
 'sequential9blau',
 'sequential5blau',
 'sequential9rot',
 'sequential5rot',
 'sequential9ocker',
 'sequential5ocker',
 'sequential9petrol',
 'sequential5petrol',
 'diverging5rotgruen',
 'diverging5rotblau',
 'gender4',
 'nation4']
In [3]:
# set the default colormap and the color cycle
plt.set_cmap('contrasting12') # or any other colormap
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=plt.get_cmap().colors)
<Figure size 432x288 with 0 Axes>

Examples

Line plot

In [4]:
# This uses the default colormap and default prop cycle defined above
x = np.linspace(0, 20, 100)
fig, ax = plt.subplots()

for i in range(10):
    ax.plot(x, i * np.cos(x))

plt.show()

Pcolormesh plot

In [5]:
# create mock data
data = np.random.random([100, 100]) * 10

plt.figure(figsize=(7, 6))
plt.pcolormesh(data, cmap='sequential9petrol')
plt.colorbar(orientation='horizontal')
Out[5]:
<matplotlib.colorbar.Colorbar at 0x27a398f4390>

Scatter plot

In [6]:
# create mock data
x,y,c = zip(*np.random.rand(30,3)*4-2)
norm = plt.Normalize(-2,2)

# scatter plot with harmonic6 colormap
plt.scatter(x,y,c=c, cmap='harmonic6', norm=norm)
plt.colorbar()
plt.show()

List of all colormaps of SSZ palette

In [7]:
# Indices to step through colormap.
x = np.linspace(0.0, 1.0, 100)

gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))


def plot_color_gradients(cmap_list):
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=len(cmap_list), ncols=1, figsize=(6.4, figh))
    fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99,
                        wspace=0.05)
    fig.suptitle('SSZ palette colormaps', fontsize=18, y=1.0, x=0.6)

    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
        pos = list(ax.get_position().bounds)
        x_text = pos[0] - 0.01
        y_text = pos[1] + pos[3]/2.
        fig.text(x_text, y_text, name, va='center', ha='right', fontsize=14)

    # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs.flat:
        ax.set_axis_off()

    plt.show()


plot_color_gradients(colorsmaps)