plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
15.99k stars 2.53k forks source link

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

Open pmantica3 opened 1 year ago

pmantica3 commented 1 year ago

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!

nicolaskruchten commented 1 year ago

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.

sergei3000 commented 1 year ago

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

gvwilson commented 2 months ago

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

sergei3000 commented 2 months ago

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