Skip to content

Log axes: a zoom re-aggregates the wrong range, and bins and line buckets ignore the log scale #98

Open

Description

@jvdd

A log axis is a Plotly layout passthrough today: fig.update_layout(xaxis={"type": "log"}) (the Figure.update_layout docstring shows it for y). The client, the spec and the engine do not know that the axis is log. This gives one bug and one design gap.

1. Bug: a zoom on a log axis re-aggregates the wrong range

Plotly reports the range of a log axis in log10 units. The relayout handler (flexviz/adapters/js/plotly/events.js, the xaxis.range[i] parsing before fvWriteViewport) stores that range in the viewport unchanged. The server reads the viewport in data units.

Reproduced in a browser on feat/heatmap-color-norm (f396838). The relayout path is the same on main. Data: x from 1 to 1e6 on a log grid, 1000 rows. A zoom to x = 100 … 10 000 (Plotly range [2, 4]):

Trace Viewport sent Data after the zoom
add_line(x="x", y="y") {"min": 2, "max": 4} 50 points with x = 2.02 … 3.99
add_histogram(x="x", bins=20) {"min": 2, "max": 4} 20 bins over x = 2.05 … 3.95

On the log axis these points sit at 0.3 … 0.6, outside the visible range, so the zoomed figure shows the wrong data. syncLayoutViewport (flexviz/adapters/js/plotly/render.js) writes the viewport back to layout.range unchanged. Today the two mistakes cancel on the client, so the axis itself looks right, and only the server gets wrong values. A fix must convert in both places.

Every trace that re-aggregates on a zoom along a log axis is affected: line (x), histogram (its value axis), histogram2d (x and y). Categorical axes and maps are not.

Not affected (checked): a box selection on a log axis. Plotly reports selections in data units, so the predicate is range: [100, 10000], and a restored selection box lands in the same place.

2. Design gap: the aggregation grid ignores the scale

Line buckets and histogram bins are linear in data units. On a log axis, the lower decades collapse. The same data, at first render:

  • Line (n_points=100): 96 points, only 1 of them below x = 10 000. Four of the six decades (667 of 1000 rows) are drawn as one straight segment.
  • Histogram (bins=20): bins 50 000 wide. The first bin holds 783 of the 1000 rows (x = 1 … 50 000, 4.7 decades).

The same holds for histogram2d grids and for the cube lattice of a cross-filter source on a log axis. Its predicates stay correct, but its snapped bins are uneven on screen.

Proposed direction

  1. Fix the bug on the client first. Convert between log10 and data units where Plotly ranges meet the viewport: relayout → viewport (10 ** v) and viewport → layout.range (Math.log10(v)), for an axis whose rendered type is log. The viewport then stays in data units, as the server, the selections and the linked axes expect. This change is small and self-contained.
  2. Then make the scale part of the spec. For example, a per-axis scale on the figure spec that the adapter maps to Plotly's axis type. This keeps it a declarative spec field, not a renderer option. With it:
    • The traces can build log-spaced line buckets, histogram bins and histogram2d grids.
    • link_axes can link log axes that share the scale (it rejects them today).
    • The cube lattice can follow the scale.

Open questions

  • API shape for step 2. For example Figure.xscale("log"), or a scale argument on the builders. Does update_layout(xaxis={"type": "log"}) stay as a second path, or does it go? The compatibility policy prefers one path.
  • Values at or below 0: Plotly hides them on a log axis. Should the traces drop them before aggregation, and should the log grid start at the smallest positive value?
  • Does a histogram on a log axis take log-spaced bins by default, or only on request?

Regression test for step 1

For tests/test_browser.py (the _color_norm_url helper from PR #95 builds a dashboard from a callback). It fails today: the viewport is {"min": 2, "max": 4}.

Browser test
    def test_zoom_on_a_log_x_axis_keeps_the_viewport_in_data_units(
        self, page: Page, server_port: int
    ):
        df = pl.DataFrame(
            {
                "x": [10 ** (6 * i / 999) for i in range(1000)],
                "y": [float(i) for i in range(1000)],
            }
        )
        url = _color_norm_url(
            server_port,
            "_browser_log_axis_zoom",
            df,
            lambda d: d.add_figure()
            .add_line(x="x", y="y", n_points=100)
            .update_layout(xaxis={"type": "log"}),
        )
        page.goto(url)
        _wait_for_init(page, "plotly")

        # Plotly reports a log axis range in log10 units: [2, 4] is x = 100 .. 10 000.
        page.evaluate("""() => Plotly.relayout(
            document.querySelector('.js-plotly-plot'),
            {'xaxis.range[0]': 2, 'xaxis.range[1]': 4})""")
        [viewport] = page.evaluate("() => flexvizState().state.viewport").values()
        assert (viewport["min"], viewport["max"]) == pytest.approx((100.0, 10_000.0))
        page.wait_for_function(
            """() => {
                const t = document.querySelector('.js-plotly-plot').data[0];
                const xs = t.x.filter(v => v != null);
                return xs.length && Math.min(...xs) >= 99 && Math.max(...xs) <= 10001;
            }""",
            timeout=10_000,
        )

Activity

  1. added
    bug
    enhancement
    on Sep 26, 2026
  2. jvdd commented on Sep 29, 2026

    @jvdd
    ContributorAuthor

    Status after #113:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions