Color maps¶
[1]:
from ipyscales import (
LinearColorScale, LogColorScale, NamedSequentialColorMap, NamedDivergingColorMap,
NamedOrdinalColorMap, ColorBar
)
[2]:
from ipyscales._example_helper import use_example_model_ids
use_example_model_ids()
[3]:
from IPython.display import display
def visualize(cm):
display(ColorBar(
colormap=cm,
length=500,
orientation='horizontal'
))
[4]:
visualize(
LinearColorScale(range=('red', 'blue'))
)
[5]:
visualize(
LogColorScale(range=('green', 'orange'))
)
[6]:
visualize(
NamedSequentialColorMap('Viridis')
)
[7]:
visualize(
NamedDivergingColorMap('RdBu', domain=(-1, 0, 1))
)
[8]:
visualize(
NamedOrdinalColorMap('Accent', domain=tuple(range(10)))
)
[9]:
import numpy as np
from ipyscales.colorarray import ArrayColorScale
visualize(
ArrayColorScale(np.array([[1, 0, 0], [0.5, 0.5, 0.5], [0, 1, 1]], dtype='float32'))
)
[10]:
visualize(
ArrayColorScale(
np.array([[0.07, 1, 0.5, 0.3], [0.16, 1, 0.5, 1.0], [0.30, 0.8, 0.4, 0.3]], dtype='float32'),
)
)
[11]:
visualize(
ArrayColorScale(
np.array([[0.07, 1, 0.5, 0.3], [0.16, 1, 0.5, 1.0], [0.30, 0.8, 0.4, 1.0]], dtype='float32'),
space='hsl',
)
)
Example use with image¶
Generate some 2D data to use as image
[12]:
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:
[13]:
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:
[14]:
from ipywidgets import HBox, VBox
VBox([
HBox([img, ColorBar(colormap=data.scale, length=len(x))]),
scale.edit()
])