Hello,
I am trying to display the y value in two formats for a scatterplot. This plot is expected to have 10,000+points (in multiple traces), and I don’t want to store 20,000 datapoints just to get the y value in two formats.
The first format is just the y value itself. The second format is the y value divided by some number (and in percent form). that number will be the same for every point in the figure.
For example, one y value may be 505, and the secondary format I want to display is: y / 500 - in percent form. So hovering over that point I would see something like: “505 (101%)”.
custom display:
custom_y = lambda y: str( round((y/500) * 100) ) + "%"
Due to the number of points I expect in each figure, I don’t want to store the y data twice). Looking at custom hovertemplates, I can’t seem to make it work. From what I can tell, I can format the y value as a percent, but not a custom percentage. EX: the hovertemplate could be: “%{y} (%{y:,.0%})”
Which will get me:
Hover, I can’t seem to find a way to apply a custom formatter to that. The only option I have found is using the “text” field, but that would generate a lot of data in the figure that I’m trying to avoid.
Is there a way to customize the hoverlabel like this? Or a custom button option that would allow me to toggle between these a display of value vs custom percentage (without duplicating the data on the backend)?
Simplified code show y and y as a percent. but I want y / 500 converted to a percentage:
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'a':[489,495,500,505,507]})
fig = px.line(df)
fig.update_traces(xhoverformat='%H:%M', yhoverformat=',.2f', hovertemplate='%{y} [%{y:,.0%}] (%{x})')
fig.show()