Color maps

In [1]:
from ipyscales import (
    LinearColorScale, LogColorScale, NamedSequentialColorMap, NamedDivergingColorMap,
    NamedOrdinalColorMap, ColorBar
)
In [2]:
from ipyscales._example_helper import use_example_model_ids
use_example_model_ids()
In [3]:
from IPython.display import display

def visualize(cm):
    display(ColorBar(
        colormap=cm,
        length=500,
        orientation='horizontal'
    ))
In [4]:
visualize(
    LinearColorScale(range=('red', 'blue'))
)
In [5]:
visualize(
    LogColorScale(range=('green', 'orange'))
)
In [6]:
visualize(
    NamedSequentialColorMap('Viridis')
)
In [7]:
visualize(
    NamedDivergingColorMap('RdBu', domain=(-1, 0, 1))
)
In [8]:
visualize(
    NamedOrdinalColorMap('Accent', domain=tuple(range(10)))
)

Example use with image

Generate some 2D data to use as image

In [9]:
import numpy as np

delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

Set up the scale to use, the color transform, and the image:

In [10]:
from ipyscales.datawidgets import ScaledArray
from ipydatawidgets import DataImage

scale = NamedSequentialColorMap(domain=(np.min(Z), np.max(Z)))
data = ScaledArray(data=Z, scale=scale, output_dtype='uint8')
img = DataImage(data=data)

Finally, display the image side-by-side with a color bar, and add a color map selector box underneath:

In [11]:
from ipywidgets import HBox, VBox

VBox([
    HBox([img, ColorBar(colormap=data.scale, length=len(x))]),
    scale.edit()
])
In [ ]: