Stuck on How to Display Hover Data in Specific Format

Viewed 100

I am testing some plotly code here.

import plotly.express as px
# find business profits
pd.options.display.float_format = '{:.2f}'.format
df_gains = df_rev_exp[((df_rev_exp.ltd_spending) < df_rev_exp.REV2)]
df_gains.tail()

# scatter plot of losses
import plotly.express as px
fig = px.scatter(df_gains, x="site_name", 
                 y="gain_or_loss", 
                 color="gain_or_loss",
                 size='REV2', hover_data=['site_name','REV2'])
fig.update_xaxes(tickangle=325)
fig.show()

Everything plots just fine but the REV2 is pretty large, and as such it is hard to read when I hover over the data points in the chart. I'm trying to figure out a way to show numbers as millions. For instance, In would like to see 1.25M and not 1257789.84, which is what I am seeing now. I tried playing around with fig.update but I couldn't get anything working. How can I modify the formatting on these plotly charts?

1 Answers

I'm on Plotly 4.14.3 and this version shows 2.2M straight out of the box when the source is x=[10000000, 22000000, 34000000]:

import numpy as np
import plotly.graph_objects as go

fig = go.Figure()
fig.add_traces(go.Scatter(x=[10*10**6, 22*10**6, 34*10**6],
                          y=[10,12,14]))
fig.show()

enter image description here

So two things come to mind:

  1. Update Plotly.
  2. Check that you're inputting your values as values and not strings
Related