Scientific colormaps for making accessible, informative and 'cmashing' plots
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Create visualizations of colormaps for evaluation, comparison, and presentation. These functions help assess colormap properties and create publication-ready overview plots.
Display single colormaps with optional test data and grayscale comparison.
def view_cmap(
cmap: str | Colormap,
*,
savefig: str | None = None,
show_test: bool = False,
show_grayscale: bool = False
) -> None:
"""
Shows a simple plot of the provided colormap.
Parameters:
- cmap: Colormap name or object to visualize
- savefig: Path to save plot (None to display)
- show_test: Show colormap test data instead of gradient
- show_grayscale: Also show grayscale version of colormap
Returns:
None
"""import cmasher as cmr
# Basic colormap visualization
cmr.view_cmap('cmr.rainforest')
# View with grayscale comparison
cmr.view_cmap('cmr.iceburn', show_grayscale=True)
# View with test data pattern
cmr.view_cmap('cmr.ocean', show_test=True)
# Save visualization to file
cmr.view_cmap('cmr.wildfire', savefig='wildfire_cmap.png')
# View all three options together
cmr.view_cmap(
'cmr.seasons',
show_test=True,
show_grayscale=True,
savefig='seasons_analysis.png'
)Create overview plots showing multiple colormaps with detailed analysis and comparison features.
def create_cmap_overview(
cmaps: list | dict | None = None,
*,
savefig: str | None = None,
use_types: bool = True,
sort: str | callable | None = "alphabetical",
show_grayscale: bool = True,
show_info: bool = False,
plot_profile: bool | float = False,
dark_mode: bool = False,
title: str | None = "Colormap Overview",
wscale: float = 1,
hscale: float = 1
) -> None:
"""
Creates an overview plot containing multiple colormaps.
Parameters:
- cmaps: Colormaps to include (None for all CMasher colormaps)
- savefig: Path to save plot (None to display)
- use_types: Categorize colormaps by type
- sort: Sorting method - 'alphabetical', 'lightness', 'perceptual', function, or None
- show_grayscale: Show grayscale versions alongside color versions
- show_info: Show lightness and perceptual statistics
- plot_profile: Plot lightness profiles (True, False, or alpha value)
- dark_mode: Use dark background and light text
- title: Plot title (None for no title)
- wscale, hscale: Scale factors for subplot dimensions
Returns:
None
"""import cmasher as cmr
# Basic overview of all CMasher colormaps
cmr.create_cmap_overview()
# Overview of specific colormap types
sequential_cmaps = cmr.get_cmap_list('sequential')
cmr.create_cmap_overview(
[f'cmr.{name}' for name in sequential_cmaps[:10]],
title="Sequential Colormaps",
savefig='sequential_overview.png'
)
# Detailed analysis with lightness profiles
cmr.create_cmap_overview(
show_info=True,
plot_profile=0.3,
sort='perceptual',
title="CMasher Colormaps - Perceptual Analysis"
)
# Dark mode overview
cmr.create_cmap_overview(
dark_mode=True,
title="CMasher Colormaps (Dark Mode)",
savefig='cmaps_dark.png'
)
# Custom colormap selection with categorical organization
custom_cmaps = {
'Ocean Theme': ['cmr.ocean', 'cmr.tropical', 'cmr.arctic'],
'Fire Theme': ['cmr.ember', 'cmr.torch', 'cmr.wildfire'],
'Nature Theme': ['cmr.jungle', 'cmr.rainforest', 'cmr.savanna']
}
cmr.create_cmap_overview(
custom_cmaps,
show_grayscale=False,
wscale=1.2,
title="Themed Colormaps"
)# Alphabetical sorting (default)
cmr.create_cmap_overview(sort='alphabetical')
# Sort by lightness profile
cmr.create_cmap_overview(sort='lightness')
# Sort by perceptual characteristics
cmr.create_cmap_overview(sort='perceptual')
# Custom sorting function
def custom_sort(cmap):
return len(cmap.name) # Sort by name length
cmr.create_cmap_overview(sort=custom_sort)
# No sorting (preserve input order)
cmr.create_cmap_overview(sort=None)import cmasher as cmr
# Compare all diverging colormaps
diverging_names = cmr.get_cmap_list('diverging')
cmr.create_cmap_overview(
[f'cmr.{name}' for name in diverging_names],
title="Diverging Colormaps Comparison",
show_info=True,
plot_profile=True,
savefig='diverging_comparison.png'
)import cmasher as cmr
# Create publication-quality overview
cmr.create_cmap_overview(
savefig='cmasher_overview_publication.png',
show_grayscale=True,
show_info=True,
plot_profile=0.25,
sort='perceptual',
title="CMasher: Scientific Colormaps for Data Visualization",
wscale=1.5,
hscale=1.2
)import cmasher as cmr
# Create focused comparison for selection
candidates = ['cmr.rainforest', 'cmr.ocean', 'cmr.tropical', 'cmr.jungle']
cmr.create_cmap_overview(
candidates,
show_grayscale=True,
show_info=True,
title="Sequential Colormap Candidates",
use_types=False # Don't group by type
)import cmasher as cmr
import matplotlib.pyplot as plt
def explore_colormap(cmap_name):
"""Interactive colormap exploration."""
print(f"\n=== {cmap_name} ===")
print(f"Type: {cmr.get_cmap_type(cmap_name)}")
# Show basic view
cmr.view_cmap(cmap_name)
# Show with test data
cmr.view_cmap(cmap_name, show_test=True)
# Extract sample colors
colors = cmr.take_cmap_colors(cmap_name, 5, return_fmt='hex')
print(f"Sample colors: {colors}")
# Explore different colormaps
for cmap in ['cmr.rainforest', 'cmr.iceburn', 'cmr.seasons']:
explore_colormap(cmap)import cmasher as cmr
import os
def create_all_cmap_views(output_dir='colormap_views'):
"""Create individual visualization for each CMasher colormap."""
os.makedirs(output_dir, exist_ok=True)
all_cmaps = cmr.get_cmap_list()
for cmap_name in all_cmaps:
filename = f"{output_dir}/{cmap_name}_view.png"
cmr.view_cmap(
f'cmr.{cmap_name}',
show_grayscale=True,
savefig=filename
)
print(f"Created: {filename}")
# Create all visualizations
create_all_cmap_views()Install with Tessl CLI
npx tessl i tessl/pypi-cmasher