I have a data frame df
and df
has two columns named calmonth
, amt_sales
.
I use box
function inplotly.express
module to generate box plot for each month.
Here is the code:
fig = px.box(
df,
y='amt_sales',
x='calmonth',
)
while the plot looks like this, as you can see the data on yaxis and hover box are scaled to M(million).
I wish to scale to ten thousands as 万 in chinese for data in yaxis and hover box .
For hover box I tried to add a new column to df
named amt_sales_converted
, then added it to hover_data
parameter.
For yaxis I used np.linspace
to cut the min and max value of amt_sales
into 10 bins and pass relative values to parameter tickvals
& ticktext
in fig.update_yaxes()
df['amt_sales_converted'] = df.amt_sales/Decimal('10000')
fig = px.box(
df,
y='amt_sales',
x='calmonth',
hover_data=['amt_sales_converted']
)
ymin = np.min(df.amt_sales.to_numpy().astype('float'))
ymax = np.max(df.amt_sales.to_numpy().astype('float'))
values = np.linspace(ymin, ymax, 10)
labels = [f"{round(v/10000, 2)}万" for v in values]
fig.update_yaxes(tickvals=values, ticktext=labels)
The result told me that it displayed scaled to ten thousands data only when i hovered on fliers and didn’t work when i hovered on the box itself.
The yaxis’s data is not a integer due to the cut in np.linspace
.
I wonder if plotly
has some configuration or functions to achieve this requirement?