Enredanrestos
Enredanrestos

Reputation: 157

Number format in plotly express hover box with error bars

I am trying to cut the number of digits displayed in the hover box.

    import plotly.express as px
    import numpy as np
    df = px.data.iris()
    df = df[df["species"]=="setosa"]
    df["sepal_length"] += np.pi/5
    df["e_plus"] = df["sepal_width"]/10*np.pi/4
    df["e_minus"] = df["sepal_width"]/4*np.pi/4
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
             error_y="e_plus", error_y_minus="e_minus")
    fig.show()

too many decimals Putting the format in the hovertemplate does not work because - although formats the y value - it removes the error bar information.

    fig.data[0]["hovertemplate"] = fig.data[0]["hovertemplate"].replace("{y}","{y:.3g}")

half solution

This needs to be a format display solution, I cannot round the data.

Upvotes: 0

Views: 204

Answers (1)

Enredanrestos
Enredanrestos

Reputation: 157

import plotly.express as px
import numpy as np
df = px.data.iris()
df = df[df["species"]=="setosa"]
df["sepal_length"] += np.pi/5
df["e_plus"] = df["sepal_width"]/10*np.pi/4
df["e_minus"] = df["sepal_width"]/4*np.pi/4
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
             error_y="e_plus", error_y_minus="e_minus", custom_data=['sepal_length','e_plus','e_minus'])
replace_string = "{customdata[0]:.3g} +%{customdata[1]:.1g}/ -%{customdata[2]:.1g}"
fig.data[0]["hovertemplate"] = fig.data[0]["hovertemplate"].replace("{y}",replace_string)
fig.show()

Upvotes: 0

Related Questions