Skip to content

enable arbitrary formatting on x-axis and y-axis tick labels #4041

Description

@pmantica3

Thanks for the awesome work on this project! It is incredibly useful!

I have a feature request. I work for a hedge fund and we want our plotly graphs to follow a standardized format. I am having a hard time through writing a generic formatting function that formats the x-axis and y-axis tick values.

For instance, if you wanted to set the tick axis to be bold and include a "x" at the end, you would need to do something like:

fig.update_layout(
    yaxis = dict(
        tickmode = 'array',
        tickvals = [1, 2, 3],
        ticktext = ["<b>1x</b>", "<b>2x</b>", "<b>3x</b>"]
    )
)

Which is pretty inconvenient because you have to specify the tickvals every time you want to do this.

So I was thinking that it could be a good idea to add something like:
fig.update_xaxis(tick_text_formatter=<function that formats a tick value>)

Thoughts?

Again, thanks for the awesome work!

Activity

  1. nicolaskruchten commented on Jan 28, 2023

    @nicolaskruchten
    Contributor

    The need for what you're asking for is very clear and your proposal makes a lot of sense from the Python developer's perspective but unfortunately this would be extremely challenging with the current architecture, as no Python code can be executed while a figure is being rendered... update_layout() and all other Python functions exclusively populate static JSON objects which are passed to Plotly.js for browser-side processing, so there would be no way for such a function to execute, and for security reasons the Plotly.js system cannot accept workaround such as stringified Javascript function in such a place either. We're basically left with the kind of formatting mini-language that we have right now, which we can/should expand to meet various missing formatting needs.

    Regarding bolding, this is also something which is missing from Plotly.js' formatting concept at the moment. We have font family and font size but not font weight in the data model, although this could be added globally to all text objects I think. Conditional bolding, much like conditional coloring of something like tick text would need to work a bit differently however.

  2. sergei3000 commented on May 31, 2023

    @sergei3000

    +1 for this kind of a feature. I came a cross a task recently where I needed to create custom format for tick values. I ended up just creating a fixed set of tick values manually but that killed the dynamic nature of the plot's ticks when zooming in on the plot which is a major drawback of my solution.

    Just for a note, the task was to create a secondary y axis ticks which would be proportional to the primary y axis ticks with a custom proportion function (something like lambda x = 1 - x / some_constatnt).

  3. gvwilson commented on Jul 11, 2024

    @gvwilson
    Contributor

    Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson

  4. sergei3000 commented on Jul 11, 2024

    @sergei3000

    @gvwilson yes, it is still a concern, see my previous comment right above yours

  5. self-assigned this
    on Jul 11, 2024
  6. removed their assignment
    on Aug 2, 2024
  7. changed the title [-]Feature Request: Arbitrary formatting on x-axis and y-axis tick labels[/-] [+]enable arbitrary formatting on x-axis and y-axis tick labels[/+] on Aug 12, 2024
  8. emilykl commented on Sep 1, 2026

    @emilykl
    Contributor

    Hi @pmantica3 @sergei3000 I'm just following up on some old issues — as noted above, we're unlikely to implement this request in the form of passing a Python function, since that would be incompatible with the existing plotting pipeline which happens entirely in JavaScript in the browser.

    However, something like ticktemplate along the lines of hovertemplate/texttemplate could possibly be implemented. It would have to be added in plotly.js though. I suppose that API would look something like:

    ticktemplate="<b>%{y}x</b>"

    Also, just a note that it's now possible (as of plotly/plotly.js#6990) to make tick labels bold, by setting tickfont.weight to 1000 (or any other number 1-1000, though not all fonts support all intermediate weights).

    You can also append a suffix to all tick labels using ticksuffix (for whatever reason, ticksuffix is ignored when tickvals is provided, but you can use tick0 and dtick instead).

    So you can get your desired outcome by using:

    fig.update_layout(
        yaxis = dict(
            tick0 = 1,
            dtick = 1,
            ticksuffix = "x",
            tickfont = dict(weight=1000)
        )
    )
    Image

    I'll leave this issue open for now though to track a desire for easier and more flexible tick formatting options.

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

    P3backlogfeaturesomething new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions