# This notebook is taken from https://github.com/QuantStack/ipygany/blob/master/examples/isocolor.ipynb
# with adaptation to use with ipyflex
import numpy as np
from ipywidgets import FloatSlider, FloatRangeSlider, Dropdown, Select, VBox, AppLayout, jslink
from ipygany import Scene, IsoColor, PolyMesh, Component, ColorBar, colormaps
from ipyflex import FlexLayout
# Create triangle indices
nx = 100
ny = 100
triangle_indices = np.empty((ny - 1, nx - 1, 2, 3), dtype=int)
r = np.arange(nx * ny).reshape(ny, nx)
triangle_indices[:, :, 0, 0] = r[:-1, :-1]
triangle_indices[:, :, 1, 0] = r[:-1, 1:]
triangle_indices[:, :, 0, 1] = r[:-1, 1:]
triangle_indices[:, :, 1, 1] = r[1:, 1:]
triangle_indices[:, :, :, 2] = r[1:, :-1, None]
triangle_indices.shape = (-1, 3)
# Create vertices
x = np.arange(-5, 5, 10/nx)
y = np.arange(-5, 5, 10/ny)
xx, yy = np.meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
vertices = np.empty((ny, nx, 3))
vertices[:, :, 0] = xx
vertices[:, :, 1] = yy
vertices[:, :, 2] = z
vertices = vertices.reshape(nx * ny, 3)
height_component = Component(name='value', array=z)
mesh = PolyMesh(
vertices=vertices,
triangle_indices=triangle_indices,
data={'height': [height_component]}
)
height_min = np.min(z)
height_max = np.max(z)
# Colorize by height
colored_mesh = IsoColor(mesh, input='height', min=height_min, max=height_max)
# Create a slider that will dynamically change the boundaries of the colormap
colormap_slider_range = FloatRangeSlider(value=[height_min, height_max], min=height_min, max=height_max, step=(height_max - height_min) / 100.)
jslink((colored_mesh, 'range'), (colormap_slider_range, 'value'))
# Create a colorbar widget
colorbar = ColorBar(colored_mesh)
# Colormap choice widget
colormap = Dropdown(
options=colormaps,
description='colormap:'
)
jslink((colored_mesh, 'colormap'), (colormap, 'index'))
scene = Scene([colored_mesh])
color = VBox([colormap, colorbar])