Skip to content

Enable logscale Colorbar - #7894

Open
bmaranville wants to merge 10 commits into
plotly:mainfrom
bmaranville:colorbar-logscale
Open

bmaranville wants to merge 10 commits into
plotly:mainfrom
bmaranville:colorbar-logscale

Conversation

@bmaranville

@bmaranville bmaranville commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Closes plotly/plotly.py#3616

Implement logscale colorbars natively

Motivation

This feature has been requested a number of times (see #7376 and https://community.plotly.com/t/logarithmic-coloraxis-in-heatmaps-not-working-as-documented/90650/5?u=matinh, for example) and our organization uses logarithmic scaling almost exclusively as the default scale for colormaps for heatmap plots, but it is currently awkward to have to manually set tick labels as suggested in the docs here:
https://plotly.com/python/colorscales/#customizing-tick-text-on-logarithmic-color-bars

Implementation

This PR

  • modifies attribute colorbar.type to be an enum with values ['linear', 'log'], defaulting to 'linear'
  • adds scaling to the range of the colorbar when 'log' is the type
  • adds tests for
    • validation of new attribute
    • proper tick labels in 'log' mode
    • proper color mappings, including for out-of-range (e.g. negative)

Note
this PR was prepared with the assistance of Gemini AI (but edited and audited thoroughly by the human me). The AI was particularly helpful in writing tests.

With this PR, the following trace:

{
  "type": "heatmap",
  "z": [
    [
      1,
      3.1622776601683795,
      10,
      31.622776601683793,
      100,
      316.22776601683796,
      1000
    ],
    [
      3.1622776601683795,
      10,
      31.622776601683793,
      100,
      316.22776601683796,
      1000,
      3162.2776601683795
    ],
    [
      10,
      31.622776601683793,
      100,
      316.22776601683796,
      1000,
      3162.2776601683795,
      10000
    ],
    [
      31.622776601683793,
      100,
      316.22776601683796,
      1000,
      3162.2776601683795,
      10000,
      31622.776601683792
    ],
    [
      100,
      316.22776601683796,
      1000,
      3162.2776601683795,
      10000,
      31622.776601683792,
      100000
    ],
    [
      316.22776601683796,
      1000,
      3162.2776601683795,
      10000,
      31622.776601683792,
      100000,
      316227.7660168379
    ],
    [
      1000,
      3162.2776601683795,
      10000,
      31622.776601683792,
      100000,
      316227.7660168379,
      1000000
    ]
  ],
  "colorscale": "Viridis",
  "colorbar": {
    "type": "log",
    "title": "Log Scale",
    "tickmode": "auto"
  }
}

renders like this:
image

@bmaranville

Copy link
Copy Markdown
Contributor Author

Note that one of the tests that is failing only because this PR introduces a minimal change to the plotly schema (colorbar.type changes from always linear to an enum with log)

Also: this addresses plotly/plotly.py#3616 and plotly/plotly.py#3735

@camdecoster

Copy link
Copy Markdown
Contributor

Thanks for the PR! This would be a great addition. Our team will take a look and follow up.

@robertclaus robertclaus assigned KoolADE85 and unassigned emilykl Sep 15, 2026
@emilykl

emilykl commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

@alexcjohnson Do you think type should be a property of coloraxis rather than colorbar ? Or both?

@alexcjohnson

Copy link
Copy Markdown
Collaborator

Thanks @bmaranville, this is a great feature! But yes I think @emilykl is on the right track that this is not a property of the colorbar but of the colorscale. So it belongs as a sibling attribute to colorscale - similar to reversescale, cmin etc, and colorbar itself - either in the trace or in coloraxis rather than inside colorbar. Aside from the conceptual "this applies to how data values map to colors, not just how the colorbar presents this mapping," the practical importance is you may use a colorscale without showing a colorbar, and it would be really awkward to ask users to create the colorbar container just to set log scale but then mark the colorbar itself as not visible.

Does make the naming a little awkward, but colorscaletype should work. Maybe inside a coloraxis this could be shortened to just type but then it would diverge from direct usage in traces which could be confusing.

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.

implement logarithmic color_continuous_scale

5 participants