Palettable
Palettable is a Python library that provides a comprehensive collection of color palettes, including those from Colorbrewer2, Tableau, CartoColors, cmocean, and more. It is written in pure Python with no external dependencies and can supply color maps for Matplotlib or be used for web applications. The current version is 3.3.3, with an irregular release cadence and maintenance releases as needed.
Warnings
- breaking The library was renamed from `brewer2mpl` to `palettable` in version 2.0.0 (released March 2015). Code using `import brewer2mpl` will fail.
- gotcha Palettes are organized into submodules (e.g., `palettable.colorbrewer.qualitative`, `palettable.matplotlib`) and follow a naming convention of `<Name>_<number of colors>` (e.g., `Dark2_7`). Reversed palettes have an `_r` suffix.
- gotcha While Palettable itself has no external *core* dependencies, certain visualization and display functions (e.g., `show_as_blocks`, `show_discrete_image`, `save_continuous_image`) require `ipythonblocks` or `matplotlib` to be installed separately.
Install
-
pip install palettable
Imports
- Dark2_7
from palettable.colorbrewer.qualitative import Dark2_7
- Viridis_256
from palettable.matplotlib import Viridis_256
Quickstart
from palettable.colorbrewer.qualitative import Dark2_7
# Access basic palette information
print(f"Palette Name: {Dark2_7.name}")
print(f"Number of Colors: {Dark2_7.number}")
# Get colors in different formats
print(f"RGB colors (0-255): {Dark2_7.colors}")
print(f"Hex colors: {Dark2_7.hex_colors}")
print(f"Matplotlib-compatible RGB (0-1): {Dark2_7.mpl_colors}")
# Example: Using with Matplotlib (requires matplotlib installed)
try:
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# Create a matplotlib colormap from the palette
cmap = ListedColormap(Dark2_7.mpl_colors)
# Display the palette as an image
fig, ax = plt.subplots(figsize=(6, 1))
ax.imshow([[i for i in range(Dark2_7.number)]], cmap=cmap, aspect='auto')
ax.set_axis_off()
ax.set_title(f"{Dark2_7.name} ({Dark2_7.number} colors)")
plt.show()
except ImportError:
print("Matplotlib not installed. Skipping visualization example.")