Heatmap histogram
The two statistical / 2D items, side by side. plot_heatmap renders a
rows × cols grid (row-major) mapped through the active colormap; histogram
bins a flat sample array into a 1D distribution and returns the largest bin count.
Together they cover the “show me the shape of this data” cases.
plot(HEAT, (title = "heatmap", size = float2(560.0f, 560.0f), flags = ImPlotFlags.None)) {
setup_axes("col", "row")
plot_heatmap("vals", g_heat, ROWS, COLS, 0.0lf, 0.0lf, "") // auto-scale, no labels
}
same_line()
plot(HIST, (title = "histogram", size = float2(-1.0f, 560.0f), flags = ImPlotFlags.None)) {
setup_axes("value", "count")
histogram("samples", g_samples, int(ImPlotBin.Sturges))
}
Source: examples/tutorial/heatmap_histogram.das.
1options gen2
2options _comment_hygiene = true
3
4require imgui/imgui_harness
5require imgui/imgui_containers_builtin
6require imgui/imgui_widgets_builtin
7require imgui/imgui_implot_boost_v2
8require implot
9require math
10
11// =============================================================================
12// TUTORIAL: heatmap_histogram — the two statistical 2D / distribution items.
13//
14// plot_heatmap(id, values, rows, cols, lo, hi, fmt) — a rows×cols grid (row-major)
15// mapped through the active colormap. lo==hi auto-scales; fmt="" hides labels.
16// histogram(id, values, bins) — bin a sample array into a
17// 1D histogram; returns the largest bin count. `bins` is a count or an
18// ImPlotBin method (Sturges / Sqrt / Rice / Scott) cast to int.
19//
20// STANDALONE: daslang.exe modules/dasImguiImplot/examples/tutorial/heatmap_histogram.das
21// LIVE: daslang-live modules/dasImguiImplot/examples/tutorial/heatmap_histogram.das
22// =============================================================================
23
24let ROWS = 14
25let COLS = 18
26let NSAMP = 600
27
28var g_ctx : ImPlotContext?
29var g_heat : array<double>
30var g_samples : array<double>
31
32[export]
33def init() {
34 harness_init("dasImguiImplot — heatmap_histogram", 1280, 720)
35 g_ctx = implot::CreateContext()
36 // A smooth 2D gradient (row-major, ROWS*COLS).
37 g_heat <- [for (idx in range(ROWS * COLS));
38 double(sin(float(idx / COLS) * 0.45f) * cos(float(idx % COLS) * 0.35f))]
39 // A roughly bell-shaped sample set (sum of sines spreads values around 0).
40 g_samples <- [for (i in range(NSAMP));
41 double(sin(float(i) * 1.3f) + sin(float(i) * 0.7f) + sin(float(i) * 2.1f))]
42}
43
44[export]
45def update() {
46 if (!harness_begin_frame()) return
47 harness_new_frame()
48
49 SetNextWindowPos(float2(20.0, 20.0), ImGuiCond.Always)
50 SetNextWindowSize(float2(1240.0, 680.0), ImGuiCond.Always)
51 window(PLOT_WIN, (text = "heatmap & histogram", closable = false,
52 flags = ImGuiWindowFlags.None)) {
53 text("plot_heatmap maps a grid through the colormap; histogram bins a sample array.")
54 plot(HEAT, (title = "heatmap", size = float2(560.0f, 560.0f),
55 flags = ImPlotFlags.None)) {
56 setup_axes("col", "row")
57 plot_heatmap("vals", g_heat, ROWS, COLS, 0.0lf, 0.0lf, "")
58 }
59 same_line()
60 plot(HIST, (title = "histogram", size = float2(-1.0f, 560.0f),
61 flags = ImPlotFlags.None)) {
62 setup_axes("value", "count")
63 histogram("samples", g_samples, int(ImPlotBin.Sturges))
64 }
65 }
66
67 harness_end_frame()
68}
69
70[export]
71def shutdown() {
72 if (g_ctx != null) {
73 DestroyContext(g_ctx)
74 }
75 harness_shutdown()
76}
77
78[export]
79def main() {
80 init()
81 while (!exit_requested()) {
82 update()
83 }
84 shutdown()
85}
Walkthrough
A guided tour of the two statistical items. The cursor sweeps diagonally across the heatmap — each cell takes its color from its value through the active colormap, so the structure in the data shows up as a pattern of colors — then glides to the histogram, which bins a flat sample array into bars whose shape is the distribution. There is no interaction to teach beyond the shapes; the recording self-verifies that both plots render and that the synthetic cursor genuinely lands hovered over the grid, so a dead frame or a missed cursor fails at teardown. A sequential colormap that runs a smooth ramp — the natural fit for a heatmap — is shown in colormaps and style.
Heatmaps
plot_heatmap(id, values, rows, cols, lo, hi, fmt) draws values (a
rows*cols row-major array) as a grid of colored cells. lo == hi (both 0)
auto-scales the color range to the data; pass explicit bounds to pin it. The format
string labels each cell with its value — fmt = "" (as here) draws no labels,
which reads better on a dense grid. Cells sample the active colormap, so wrapping a
heatmap in with_colormap recolors it.
Histograms
histogram(id, values, bins) bins values and returns the largest bin count
(or density, with the Density flag). bins is a positive count or an
ImPlotBin method — Sturges / Sqrt / Rice / Scott — cast to int,
which picks the bin count from the sample size. An all-zero range auto-ranges to the
data. (The name is histogram, not plot_histogram: dasImgui already owns that
widget name for ImGui’s sparkline.)