Skip to content

feat(heatmap): add color_norm with a log option to histogram2d and geo_histogram2d - #95

Merged
jvdd merged 2 commits into
mainfrom
feat/heatmap-color-norm
Sep 27, 2026
Merged

jvdd merged 2 commits into
mainfrom
feat/heatmap-color-norm

Conversation

@jvdd

@jvdd jvdd commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

A density heatmap of a large dataset is often dominated by a few dense bins. With a linear color scale, most of the map then stays dark. This PR adds color_norm to add_histogram2d and add_geo_histogram2d: "linear" (default) or "log".

fig.add_histogram2d(x="lon", y="lat", x_bins=1200, y_bins=1140, color_norm="log")

Design

  • color_norm is a style hint in TraceSpec.display, next to color_scale and color_range. The traces validate it. Aggregation and deltas do not change, so a delta still carries raw values.
  • Plotly has no log color axis, so the client colors by log10(z) (applyLogColorNorm in plotly/traces.js). It runs when a trace is built from its template, so it covers every delta, from the server or the cube. The template keeps a fixed color_range in data units.
  • Only the colors change:
    • The hover reads the raw values from text, formatted with .10~r (so 0.15, not 0.15000000000000002). An empty cell hovers as NaN, as with the linear norm.
    • A cell at or below 0 has no log and no color, but it keeps its hover and its linked hover.
    • On a map, such a cell is drawn with opacity 0 and the lowest z, so a box selection over it still keeps its rows.
    • Plotly does not hover a heatmap in which every cell is a gap. So when no heatmap cell has a color, the whole trace turns transparent instead.
  • The color range is pinned in log10 space: a fixed range from its data units, an auto range from the colored cells, a single value as a narrow band, and a nominal range around 1 when no cell is above 0.
  • The colorbar ticks sit at round data values and are labelled with them: decades (1, 10, 100, 1K, thinned to at most eight), else 1-2-5 steps, else linear steps for a narrow range (99, 99.5, 100, …). Below 0.001 and from 1e15 up, the labels use an exponent (1e-7, 1e15).
  • A fixed color_range must be above 0 with a log norm.
  • A decoded spec reaches the Plotly adapter without the trace validation. So the adapter checks color_norm with the same helper as the traces, and it rejects a log norm on corr_heatmap. Decoded specs skip trace validation: invalid display values give HTTP 500 at /view, and /share accepts them #99 moves this validation to the server.
  • All heatmaps in one figure must share color_norm, like color_scale and color_range. The check now includes geo_histogram2d, which it skipped before (separate commit). CorrHeatmap has no color_norm and stays linear. ECharts ignores it.
  • The overlay sync keeps the range of a layer that already has one: a fixed range from the template, or the range that the log norm pins.
  • The name is color_norm, not log_color: bool, so a later norm (for example equal-histogram) needs no new parameter.
  • No spec version bump: the key is optional, and a missing key means linear.

Performance

A 1320 × 1150 grid in Chromium, median of 5 renders, log compared with linear:

  • Base mode, full render: 269 ms vs 257 ms.
  • Overlay mode (two layers), full render: 1036 ms vs 921 ms.

Building a log layer costs about 60 ms more than a linear one. About half of that is the copy of the raw values for the hover. One pass builds both grids. Plotly renders a pinned range a little faster, which recovers part of the cost.

Tests

  • Unit tests:
    • The helper validation.
    • Trace round-trips (linear and log) and a legacy spec without the key.
    • The Figure API, a mixed-norm figure, and a mixed-style geo_histogram2d figure.
    • The adapter: a fixed log range stays in data units. An invalid, misplaced or <= 0 imported style is rejected with the right message.
  • Browser tests:
    • Log z, the raw hover label and the count ticks on a heatmap.
    • Cells at or below 0 and an empty cell keep their hover and their linked hover guide.
    • A heatmap without a colored cell keeps its hover.
    • A geo heatmap: log colors, the raw hover label and 1-2-5 ticks.
    • Geo cells at or below 0 stay hoverable, and a real box drag over them keeps their rows.
    • Tick positions and labels: decades, thinning, 1-2-5 steps, linear steps, a single value, and small and large values.
    • A uniform count map (one tick) and a narrow fixed range, end to end.
    • An overlay cross-filter with an auto and a fixed range, on both layers.
  • make test: 2390 passed, 2 skipped. make test-browser: 313 passed, 9 skipped. mkdocs build --strict passes.
  • The new hover, linked-hover, map-selection and tick tests fail on the previous head of this PR (f396838).

Not changed

@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing feat/heatmap-color-norm (e6803c8) with main (f0b8e7c)

Open in CodSpeed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Preserve positional API compatibility and validate unsupported decoded normalization values.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Medium severity

Open (2)
What changed in this PR

Adds optional logarithmic color normalization for 2D and geographic heatmaps, including Plotly transforms, raw hover values, data-unit ticks, tests, and documentation.

Changes:

  • Adds color_norm="linear"|"log" to histogram APIs and specifications.
  • Handles log-space coloring, ranges, ticks, and overlays.
  • Adds unit/browser coverage and documentation updates.

Required changes:

  • Preserve the existing positional axes argument order in Figure and Histogram2D.
  • Reject unsupported decoded normalization values instead of silently rendering linearly.
File Summary
tests/​test_trace_hist2d.py Tests histogram normalization and round trips.
tests/​test_trace_geo_hist2d.py Tests geographic histogram normalization.
tests/​test_spec.py Tests serialization and mixed styles.
tests/​test_html_adapters.py Tests Plotly range conversion.
tests/​test_hist_helpers.py Tests normalization validation.
tests/​test_browser.py Tests rendering, ticks, hover, and overlays.
flexviz/​trace/​hist2d.py Adds histogram normalization support.
flexviz/​trace/​geo_hist2d.py Adds geographic normalization support.
flexviz/​trace/​_hist_helpers.py Validates normalization options.
flexviz/​spec.py Extends display metadata.
flexviz/​figure.py Exposes the API and validates styles.
flexviz/​adapters/​plotly_adapter.py Converts fixed ranges to log space.
flexviz/​adapters/​js/​plotly/​traces.js Applies log coloring and custom ticks.
CHANGELOG.md Documents the feature.
Architecture.md Updates architecture documentation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread flexviz/figure.py Resolved
Comment thread flexviz/trace/hist2d.py Resolved
@jvdd
jvdd force-pushed the feat/heatmap-color-norm branch from f396838 to d62f944 Compare September 27, 2026 09:09
@jvdd
jvdd force-pushed the feat/heatmap-color-norm branch from d62f944 to e6803c8 Compare September 27, 2026 09:16
@jvdd
jvdd merged commit 591171d into main Sep 27, 2026
18 checks passed
@jvdd
jvdd deleted the feat/heatmap-color-norm branch September 27, 2026 10:47
@jvdd jvdd mentioned this pull request Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants