I have gone through this, this and some other similar posts. They all give the solution in javascript which I am unable to port to python. Help would be appreciated.
When I pass a value to customdata
which is big enough to be a billion, it shows the sign as G (for giga) rather than B, whereas plotly defaults to B, the comparison can be seen in the image. Is there a way to use B instead of G?
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x':[500,3000,50000,100000,7000000,80000000,400000000,9000000000]})
more_data = [100,3000,50000,600000,2000000,90000000,500000000,3000000000]
fig = px.line(df, log_y=True)
fig.update_traces(mode='lines+markers',
customdata=more_data,
hovertemplate='%{y}<br>%{customdata:,.1s}')
This is not going to be the most elegant solution ever but it fixes your problem. If you look carefully your plot you have a problem on index 1 too where the overdata is 3000<br>3k
.
So I'm actually writing hover data explicitly. We will need the function human_format
from here.
import pandas as pd
import plotly.express as px
df = pd.DataFrame({'x': [500,3000,50000,100000,7000000,80000000,400_000_000,9_000_000_000],
'more_data': [100,3000,50000,600000,2000000,90000000,500000000,3_000_000_000]})
def human_format(num):
num = float('{:.3g}'.format(num))
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'),
['', 'K', 'M', 'B', 'T'][magnitude])
df["hover_data"] = df.apply(lambda r:
f"{human_format(r['x'])}<br>{human_format(r['more_data'])}",
axis=1)
fig = px.line(df,
y="x", log_y=True)
fig.update_traces(mode='lines+markers',
customdata=df['hover_data'],
hovertemplate='%{customdata}')