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
 2
 3require imgui/imgui_harness
 4require imgui/imgui_containers_builtin
 5require imgui/imgui_widgets_builtin
 6require imgui/imgui_implot_boost_v2
 7require implot
 8require math
 9
10// =============================================================================
11// TUTORIAL: heatmap_histogram — the two statistical 2D / distribution items.
12//
13//   plot_heatmap(id, values, rows, cols, lo, hi, fmt)  — a rows×cols grid (row-major)
14//       mapped through the active colormap. lo==hi auto-scales; fmt="" hides labels.
15//   histogram(id, values, bins)                        — bin a sample array into a
16//       1D histogram; returns the largest bin count. `bins` is a count or an
17//       ImPlotBin method (Sturges / Sqrt / Rice / Scott) cast to int.
18//
19// STANDALONE: daslang.exe modules/dasImguiImplot/examples/tutorial/heatmap_histogram.das
20// LIVE:       daslang-live modules/dasImguiImplot/examples/tutorial/heatmap_histogram.das
21// =============================================================================
22
23let ROWS = 14
24let COLS = 18
25let NSAMP = 600
26
27var g_ctx : ImPlotContext?
28var g_heat : array<double>
29var g_samples : array<double>
30
31[export]
32def init() {
33    harness_init("dasImguiImplot — heatmap_histogram", 1280, 720)
34    g_ctx = implot::CreateContext()
35    // A smooth 2D gradient (row-major, ROWS*COLS).
36    g_heat <- [for (idx in range(ROWS * COLS));
37               double(sin(float(idx / COLS) * 0.45f) * cos(float(idx % COLS) * 0.35f))]
38    // A roughly bell-shaped sample set (sum of sines spreads values around 0).
39    g_samples <- [for (i in range(NSAMP));
40                  double(sin(float(i) * 1.3f) + sin(float(i) * 0.7f) + sin(float(i) * 2.1f))]
41}
42
43[export]
44def update() {
45    if (!harness_begin_frame()) return
46    harness_new_frame()
47
48    SetNextWindowPos(float2(20.0, 20.0), ImGuiCond.Always)
49    SetNextWindowSize(float2(1240.0, 680.0), ImGuiCond.Always)
50    window(PLOT_WIN, (text = "heatmap & histogram", closable = false,
51                      flags = ImGuiWindowFlags.None)) {
52        text("plot_heatmap maps a grid through the colormap; histogram bins a sample array.")
53        plot(HEAT, (title = "heatmap", size = float2(560.0f, 560.0f),
54                    flags = ImPlotFlags.None)) {
55            setup_axes("col", "row")
56            plot_heatmap("vals", g_heat, ROWS, COLS, 0.0lf, 0.0lf, "")
57        }
58        same_line()
59        plot(HIST, (title = "histogram", size = float2(-1.0f, 560.0f),
60                    flags = ImPlotFlags.None)) {
61            setup_axes("value", "count")
62            histogram("samples", g_samples, int(ImPlotBin.Sturges))
63        }
64    }
65
66    harness_end_frame()
67}
68
69[export]
70def shutdown() {
71    if (g_ctx != null) {
72        DestroyContext(g_ctx)
73    }
74    harness_shutdown()
75}
76
77[export]
78def main() {
79    init()
80    while (!exit_requested()) {
81        update()
82    }
83    shutdown()
84}

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.)