How to customize hover information depending on variable value

Viewed 16

I have a dataframe with several columns. I'm interested in displaying some of this info when hovering over a specific point using plotly with python. I've discovered how to do this using customdata=dataframme[["A","B"]] being provided to my go.scatter. Problem is that when I create hovertemplate, it could happen I don't want to display some info it that value is not available. This is the code snippet :

fig.add_trace(
        go.Scatter(x=df['Request time'], y=df['Taken time'], name="System performance", hovertemplate=
        "<br>Request period: %{text}<br>" +
        "Taken time: %{y}<br>" +
        "Error msg: %{customdata[0]}<br>" +
        "LogCorr: %{customdata[1]}<br>",
                   customdata=df[["ai","logCorr"]],
                   text=df['Request period']),
        secondary_y=True,
    )

This is working fine, but if I leave this like it is, when "ai" value is empty, I see quite ugly hover

enter image description here

I was thinking if there is some way to display hover info when column value exists (in my case) and display nothing when it doesn't Something so "easy" like "" if X==0 else "Error message : %{customdata[1]} in hovertemplate. This is not working due to customdata means nothing to python in execution time. I've tried for looking which language is used in hovertemplate but no luck

Thanks a lot for your help

0 Answers
Related