Colormaps#

VisualAstro provides modules for working with colormaps. VisualAstro registers its colormaps with matplotlib at import time

The examples on this page assume:

import visualastro as va

Accessing Colormaps#

Retrieve a colormap with get_cmap(). It is a thin wrapper around get_cmap() with additional options:

  • cmap_range: (min, max) fraction of the colormap to keep

  • bad_color: color for masked or NaN values

  • under_color: color for values below the normalization minimum

  • over_color: color for values above the normalization maximum

The example below truncates turbo to the range 0.1 to 0.9 and sets custom colors for masked, under-range, and over-range values.

import matplotlib as mpl
import matplotlib.pyplot as plt

import visualastro as va

cmap = va.get_cmap(
    'turbo', cmap_range=(.1,.9), bad_color='grey', under_color='b', over_color='r'
)

fig, ax = plt.subplots(figsize=(6, 0.8))
fig.subplots_adjust(bottom=0.5)
norm = mpl.colors.Normalize(0, 1)
fig.colorbar(
    mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
    cax=ax, orientation='horizontal', extend='both'
)

(Source code, svg)

../_images/plot_get_cmap.svg

Creating Colormaps#

Create a continuous or discrete colormap with create_cmap(). You can create the colormap out of a sequence of colors or an existing VisualAstro colormap.

import matplotlib as mpl
import matplotlib.pyplot as plt

import visualastro as va

cmap = va.create_cmap(['3 train', 'violetred', '7 train', 'Holy Blue', 'A train'], name='mycmap')

fig, ax = plt.subplots(figsize=(6, 0.8))
fig.subplots_adjust(bottom=0.5)
norm = mpl.colors.Normalize(0, 1)
fig.colorbar(
    mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
    cax=ax, orientation='horizontal', extend='both'
)

(Source code, svg)

../_images/plot_create_cmap.svg

Discrete colormap:

import matplotlib as mpl
import matplotlib.pyplot as plt

import visualastro as va

cmap = va.create_cmap('astro_seq', kind='discrete')

fig, ax = plt.subplots(figsize=(6, 0.8))
fig.subplots_adjust(bottom=0.5)
norm = mpl.colors.Normalize(0, 1)
fig.colorbar(
    mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
    cax=ax, orientation='horizontal', extend='both'
)

(Source code, svg)

../_images/plot_create_cmap_discrete.svg

Plot All Colormaps#

Plot every colormap currently registered with matplotlib with cmap().

import visualastro as va

va.help.cmap()

(Source code, svg)

../_images/plot_all_cmaps.svg

Plot Colormap Lightness#

Warning

This requires the optional package colorspacious. See Installation Guide.

Plot the lightness \(L^*\) values of a colormap, similar to the plots by matplotlib.

import matplotlib.pyplot as plt

import visualastro as va

plt.style.use(['full', 'cm'])
fig, ax = plt.subplots(2, 1, figsize=(14,10))
ax[0].set_title('Perceptually Uniform Colormaps')
va.plot_cmap_lightness(['viridis', 'plasma', 'inferno', 'cividis'], ncols=6, offset=.2, ax=ax[0])
va.plot_cmap_lightness(['turbo', 'jet', 'rainbow'], ncols=5, offset=.2, ax=ax[1])
plt.show()

(Source code, svg)

../_images/plot_cmap_L.svg