brewer2mpl
brewer2mpl is a Python package that provides access to the ColorBrewer2.org color maps. It allows users to retrieve sequential, diverging, and qualitative color palettes in various formats, including RGB 0-255, hex strings, and 0-1 normalized RGB triplets suitable for Matplotlib. The library is no longer actively maintained; its functionality has been superseded by the `Palettable` library. The current version is 1.4.1, released in 2014, and there are no further updates planned.
Common errors
-
ValueError: zero length field name in format
cause This error specifically occurred in Python 2.6 due to a change in string formatting syntax. It was encountered when `brewer2mpl` used `{:>02}` which is not supported in Python 2.6.fixUpgrade to a newer Python 2.x version (2.7+) or Python 3.x. If stuck on Python 2.6, manually edit `brewer2mpl.py` (line 151) to replace `{:>02}` with `{0:>02}`. -
KeyError: ('Invalid color map or type', 'NonExistentMap', 'Qualitative', 5)cause This error occurs when `brewer2mpl.get_map()` is called with an invalid color map name, an incorrect type (e.g., 'Qualitative' when the map is 'Sequential'), or a number of colors not available for that specific map/type combination. ColorBrewer2 maps have specific names, types, and a limited range of available color counts (e.g., 3-12 colors).fixAlways verify the correct map name, type (case-sensitive 'Sequential', 'Diverging', 'Qualitative'), and the available number of colors by checking the ColorBrewer2.org website or using `brewer2mpl.print_maps()` to list available options.
Warnings
- deprecated The `brewer2mpl` library is no longer actively maintained or updated. Its development ceased in 2014, and it has been officially superseded by the `Palettable` library. Users are strongly encouraged to migrate to `Palettable` for ongoing support, new features, and broader palette options.
- gotcha When working with Matplotlib, ensure you use the correct color representation from a `BrewerMap` object. `bmap.colors` provides RGB as 0-255 integer triplets, while `bmap.mpl_colors` provides RGB as 0-1 float triplets, which is the standard expected by Matplotlib functions for discrete colors. `bmap.mpl_colormap` provides a `matplotlib.colors.LinearSegmentedColormap` object for continuous usage.
Install
-
pip install brewer2mpl
Imports
- brewer2mpl
import brewer2mpl
- get_map
bmap = brewer2mpl.get_map('Set1', 5)bmap = brewer2mpl.get_map('Set1', 'Qualitative', 5) - sequential
from brewer2mpl.sequential import Blues
Quickstart
import brewer2mpl
import matplotlib.pyplot as plt
# Get a qualitative color map named 'Set2' with 7 colors
bmap = brewer2mpl.get_map('Set2', 'Qualitative', 7)
# Access colors as 0-1 RGB triplets for Matplotlib
colors_for_mpl = bmap.mpl_colors
print(f"Color map name: {bmap.name}")
print(f"Number of colors: {bmap.number}")
print(f"Matplotlib compatible colors (0-1 RGB): {colors_for_mpl}")
# Example of using colors in a Matplotlib plot (if matplotlib is installed)
# plt.figure(figsize=(7, 1))
# for i, color in enumerate(colors_for_mpl):
# plt.barh(0, 1, left=i, color=color)
# plt.axis('off')
# plt.title('Brewer2mpl Set2 Qualitative (7 colors)')
# plt.show()