Install glasbey via pip
mainYou can install the glasbey library using pip. The library requires numba, numpy, colorspacious, and matplotlib as dependencies.
pip install glasbeyrepository·main·Indexed 42 days ago
https://github.com/lmcinnes/glasbeyA library for algorithmically generating categorical color palettes designed for maximum visual distinctiveness. Version 0.3.0 provides tools to create, extend, and constrain palettes using perceptual color spaces, including support for color-blind safe palettes (CVD simulations for deuteranomaly, protanomaly, and tritanomaly) and block palettes for hierarchical data. Key functions include `create_palette`, `extend_palette`, `create_block_palette`, and `extend_block_palette`.
You can install the glasbey library using pip. The library requires numba, numpy, colorspacious, and matplotlib as dependencies.
pip install glasbeyIf optimize_palette is enabled, Glasbey performs a greedy hill-climbing optimization on the generated palette. It attempts to maximize the perceptual distance between all colors in the set.
optimize_palette_n_iter controls how many iterations the optimizer runs.optimize_palette_search_radius defines the maximum distance in CAM02-UCS space that a color can be moved to find a better position.Note: A larger search radius may result in colors moving away from their intended 'thematic block' center to improve the overall palette's distinctness, while a smaller radius keeps colors closer to their block centers.
When colorblind_safe=True is passed to create_palette, extend_palette, or create_block_palette, the algorithm selects colors based on perceptual distances in a simulated color vision deficient (CVD) space (using CAM02-UCS).
Available settings:
cvd_type: The type of deficiency to simulate. Options are:"protanomaly": Red-green deficiency."deuteranomaly": Most common red-green deficiency."tritanomaly": Blue/yellow deficiency.cvd_severity (float): A value between 0 (regular vision) and 100 (complete dichromacy).Glasbey creates palettes by sampling from a grid of colours. You can tune the trade-off between speed and colour distinctiveness using these parameters:
grid_size: Specifies the side length of a cube-shaped grid. A larger grid_size increases the number of available colours ($grid_size^3$), improving quality but increasing computation time. You can pass a single integer or a tuple (L, C, H) to specify different side lengths for lightness, chroma, and hue.grid_space: Defines the colour space used for sampling. Supported values are "RGB" and "JCh" (useful for uniform sampling across lightness/chroma/hue).# High-quality sampling using JCh space and custom grid dimensions
glasbey.create_palette(palette_size=12, grid_size=(64, 32, 256), grid_space="JCh")When colorblind_safe=True is passed to extend_block_palette, the algorithm adjusts its distance calculations to account for color vision deficiency (CVD).
Instead of using standard perceptual distances, it simulates the user's vision using the colorspacious library. It converts the sampling grid into a simulated CVD color space (e.g., sRGB1+CVD) and calculates distances within that space. This ensures that the colors selected are perceptually distinct for individuals with the specified cvd_type (protanomaly, deuteranomaly, or tritanomaly) at the specified cvd_severity.
Glasbey provides features to make categorical colour palettes more accessible to individuals with colour vision deficiency (CVD). This is achieved by biasing the palette selection process to favor colours that are more easily distinguishable under specific CVD simulations.
To enable this, set colorblind_safe=True in any of the standard Glasbey functions: create_palette, extend_palette, or create_block_palette.
import glasbey
import seaborn as sns
# Create a basic colour-blind safe palette
palette = glasbey.create_palette(palette_size=12, colorblind_safe=True)
sns.palplot(palette)When generating colour-blind safe palettes, the optimization process (hill-climbing) can be difficult because it measures distances in a colour-vision-deficient perceptual space while selecting neighbors in the full colour space.
To improve distinguishability, you can increase the optimize_palette_search_radius parameter. Increasing this value helps the optimizer find better candidates for distinction.
# Example: Increasing search radius to improve optimization
palette = glasbey.create_palette(
palette_size=12,
colorblind_safe=True,
optimize_palette_search_radius=50
)Use glasbey.create_palette() to generate a new palette of a specific size. You can also apply constraints to the palette's appearance using lightness_bounds and chroma_bounds to create muted or specific styles.
import glasbey
# Create a categorical palette with 15 colours
glasbey.create_palette(palette_size=15)
# Create a muted palette with 12 colours
glasbey.create_palette(palette_size=12, lightness_bounds=(20, 40), chroma_bounds=(40, 50))Use glasbey.create_palette() to generate a new palette of a specific size. You can also apply constraints to the palette's appearance using lightness_bounds and chroma_bounds to control the perceptual look of the colours.
import glasbey
# Create a categorical palette with 15 colours
glasbey.create_palette(palette_size=15)
# Create a muted palette with 12 colours using lightness and chroma constraints
glasbey.create_palette(palette_size=12, lightness_bounds=(20, 40), chroma_bounds=(40, 50))Use glasbey.extend_palette() to grow an existing palette. You can extend a palette by providing a string name of a predefined palette (like matplotlib's tab10) or by providing a list of specific hex colour strings to act as seeds. The palette_size parameter determines the total number of colours in the resulting palette.
import glasbey
# Add an extra 5 colours to matplotlib's tab10 palette (total 15)
glasbey.extend_palette("tab10", palette_size=15)
# Seed a palette with specific initial colours to reach a target size of 8
glasbey.extend_palette(["#2a3e63", "#7088b8", "#fcaf3e", "#b87088"], palette_size=8)The glasbey package provides three primary functions for generating and manipulating categorical color palettes designed for maximum visual distinction:
create_palette(n_colors, ...): Generates a new categorical palette containing n_colors.extend_palette(palette, n_colors, ...): Takes an existing palette and adds n_colors new, maximally distinct colors to it.create_block_palette(n_colors, ...): Generates a block palette (likely for specific grouping or block-based visualization needs).These functions are the core entry points for generating color schemes that ensure high contrast between adjacent colors in categorical data visualizations.
import glasbey
# Example usage patterns
# palette = glasbey.create_palette(n_colors=15)
# extended_palette = glasbey.extend_palette(palette, n_colors=5)
# block_palette = glasbey.create_block_palette(n_colors=10)You can use extend_palette to build a thematic categorical palette starting from a specific seed.
lightness_bounds to tune the result.# Build a palette of 12 colours around a single hex code
palette_from_single = glasbey.extend_palette("#2a3e63", palette_size=12, lightness_bounds=(10, 40))
# Build a palette of 8 colours from a specific list of hex codes
palette_from_list = glasbey.extend_palette(["#2a3e63", "#7088b8", "#fcaf3e", "#b87088"], palette_size=8)