1
for design in list
    if len(design) > 12:
        df["Name"] = str(design)[:12] + str("...")

    fig.add_trace(
        go.Box(
            x=df["Name"],
            y=df[y_variable],
            name="",
            quartilemethod=quartile_method,
            boxpoints=False,
        )

so if i have 2 names in the list name1 = ABCDEFGHIJKL name2 = ABCDEFGHIJKLM

after truncation name1 = ABCDEFGHIJKL name2 = ABCDEFGHIJKL

so both become same in x.

Overlapping box blot with same x

How to overcome this? I tried using unique identifier but at the same time I want to have same in the x axis

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Please provide a [reproducible minimal example](https://stackoverflow.com/q/20109391/8107362). Especially, provide some [sample data](https://stackoverflow.com/q/22418895/8107362), e.g. with `print(df.to_dict())`. – mnist Oct 27 '21 at 17:41

1 Answers1

1
import plotly.express as px
import plotly.graph_objects as go

df = px.data.tips()
df["name"] = df["time"].map({"Dinner": "ABCDEFGHIJKLMNO", "Lunch": "ABCDEFGHIJKLMNOP"})

go.Figure(
    [
        go.Box(
            x=df.loc[df["name"].eq(n), "name"].str[0:12],
            y=df.loc[df["name"].eq(n), "total_bill"],
            name=n,
            boxpoints=False
        )
        for n in df["name"].unique()
    ]
).show()


go.Figure(
    [
        go.Box(
            x=df.loc[df["name"].eq(n), "name"],
            y=df.loc[df["name"].eq(n), "total_bill"],
            name=n,
            boxpoints=False
        )
        for n in df["name"].unique()
    ]
).update_layout(xaxis=dict(tickmode="array", tickvals=df.loc[:, "name"], ticktext=df.loc[:, "name"].str[0:12]))

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Is there any possibility to append/remove text from hover text coming over the plot? can we show different text in hover text from x ticks text like ABCDEFGHIJK. ex. having ABCDEFGHIJK in x axis ticks but ABCDEFGHIJK123 in hovertext over the plot – Sumit Kumar Oct 31 '21 at 18:48
  • your sample code was using graph objects so I used. `px.box(df, x=df["name"].str[0:12], y="total_bill", color="name", hover_name="name", points="all")` is same solution with Plotly Express . the hover info is only on points scatter, not the quartiles. it's obviously in the color and legend – Rob Raymond Oct 31 '21 at 19:20
  • Thanks @Rob Raymond – Sumit Kumar Dec 13 '21 at 13:33
  • is it possible to have a boxplot for single point?https://stackoverflow.com/questions/70970008/boxplot-for-single-point – Sumit Kumar Feb 03 '22 at 10:57