Palettable

In [2]:
%matplotlib inline
In [3]:
import matplotlib.pyplot as plt
import numpy as np

Palettable API

In [4]:
from palettable.colorbrewer.qualitative import Set1_9
In [5]:
Set1_9.name
Out[5]:
'Set1'
In [6]:
Set1_9.type
Out[6]:
'qualitative'
In [7]:
Set1_9.number
Out[7]:
9
In [8]:
Set1_9.colors
Out[8]:
[[228, 26, 28],
 [55, 126, 184],
 [77, 175, 74],
 [152, 78, 163],
 [255, 127, 0],
 [255, 255, 51],
 [166, 86, 40],
 [247, 129, 191],
 [153, 153, 153]]
In [9]:
Set1_9.hex_colors
Out[9]:
['#E41A1C',
 '#377EB8',
 '#4DAF4A',
 '#984EA3',
 '#FF7F00',
 '#FFFF33',
 '#A65628',
 '#F781BF',
 '#999999']
In [10]:
Set1_9.mpl_colors
Out[10]:
[(0.8941176470588236, 0.10196078431372549, 0.10980392156862745),
 (0.21568627450980393, 0.49411764705882355, 0.7215686274509804),
 (0.30196078431372547, 0.6862745098039216, 0.2901960784313726),
 (0.596078431372549, 0.3058823529411765, 0.6392156862745098),
 (1.0, 0.4980392156862745, 0.0),
 (1.0, 1.0, 0.2),
 (0.6509803921568628, 0.33725490196078434, 0.1568627450980392),
 (0.9686274509803922, 0.5058823529411764, 0.7490196078431373),
 (0.6, 0.6, 0.6)]
In [11]:
Set1_9.mpl_colormap
Out[11]:
<matplotlib.colors.LinearSegmentedColormap at 0x106725d30>
In [12]:
# requires ipythonblocks
Set1_9.show_as_blocks()
In [13]:
Set1_9.show_continuous_image()
In [14]:
Set1_9.show_discrete_image()

Setting the matplotlib Color Cycle

Adapted from the example at http://matplotlib.org/examples/color/color_cycle_demo.html. Use the .mpl_colors attribute to change the color cycle used by matplotlib when colors for plots are not specified.

In [3]:
from palettable.wesanderson import Aquatic1_5, Moonrise4_5
In [4]:
x = np.linspace(0, 2 * np.pi)
offsets = np.linspace(0, 2*np.pi, 4, endpoint=False)
# Create array with shifted-sine curve along each column
yy = np.transpose([np.sin(x + phi) for phi in offsets])

plt.rc('lines', linewidth=4)
plt.rc('axes', color_cycle=Aquatic1_5.mpl_colors)

fig, (ax0, ax1)  = plt.subplots(nrows=2)

ax0.plot(yy)
ax0.set_title('Set default color cycle to Aquatic1_5')

ax1.set_color_cycle(Moonrise4_5.mpl_colors)
ax1.plot(yy)
ax1.set_title('Set axes color cycle to Moonrise4_5')

# Tweak spacing between subplots to prevent labels from overlapping
plt.subplots_adjust(hspace=0.3)

Using a Continuous Palette

Adapted from http://matplotlib.org/examples/pylab_examples/hist2d_log_demo.html. Use the .mpl_colormap attribute any place you need a matplotlib colormap.

In [5]:
from palettable.colorbrewer.sequential import YlGnBu_9
In [9]:
from matplotlib.colors import LogNorm

#normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000)+5

plt.hist2d(x, y, bins=40, norm=LogNorm(), cmap=YlGnBu_9.mpl_colormap)
plt.colorbar()
Out[9]:
<matplotlib.colorbar.Colorbar at 0x107b0eda0>

Note that matplotlib already has colorbrewer palettes, as you can see at http://matplotlib.org/examples/color/colormaps_reference.html. Above I could have used cmap=plt.cm.YlGnBu for the same affect.

In [ ]: