Skip to main content
v2.2

Heatmap

HeatmapHeatmap
import numpy as np
import lightningchart as lc

# Set your LightningChart license key
lc.set_license('my-license-key')

# Define a 3D numpy array with random values
array_3d = np.random.normal(loc=0, scale=1, size=(100, 100, 100))

# Define a 2D slice
slice_data = array_3d[50, :, :]

# Convert the slice data to a float array
slice_data = slice_data.astype(float)
slice_data[slice_data == 0] = np.nan

# Create a new chart
chart = lc.ChartXY(
title='Heatmap',
theme=lc.Themes.Light
)

# Create the heatmap grid series
grid_size_x, grid_size_y = slice_data.shape
heatmap_series = chart.add_heatmap_grid_series(
columns=grid_size_x,
rows=grid_size_y,
)

# Set the start, end, and step positions
heatmap_series.set_start(x=0, y=0)
heatmap_series.set_end(x=grid_size_x, y=grid_size_y)

# Set the step size and enable interpolation
heatmap_series.set_step(x=1, y=1)
heatmap_series.set_intensity_interpolation(True)
heatmap_series.invalidate_intensity_values(slice_data.tolist())

# Hide wireframe
heatmap_series.hide_wireframe()

# Define custom palette for the heatmap
custom_palette = [
{"value": np.nanmin(slice_data), "color": ('blue')},
{"value": np.nanpercentile(slice_data, 25), "color": ('cyan')},
{"value": np.nanmedian(slice_data), "color": ('green')},
{"value": np.nanpercentile(slice_data, 75), "color": ('yellow')},
{"value": np.nanmax(slice_data), "color": ('red')}
]

# Set the color palette to the heatmap
heatmap_series.set_palette_coloring(
steps=custom_palette,
look_up_property='value',
interpolate=True,
formatter_precision=2
)

# Set axis titles
chart.get_default_x_axis().set_title('X-axis')
chart.get_default_y_axis().set_title('Y-axis')

chart.open()