Modify only some traces in Plotly hoverinfo or keep default trendline hoverinfo

Viewed 24

I want to edit the hoverinfo of my markers, while keeping the trendline hoverinfo as the default, or suppressing it altogether. My solution currently returns nothing for the trendline because it doesn't have the necessary criteria.

I would like it to show the default regression information, or 'hide' while using custom_data for the markers.

df = pd.DataFrame({'Expected': {0: 0.119679,
                  1: 0.11389,
                  2: 0.108821,
                  3: 0.10432999999999999,
                  4: 0.10030800000000001,
                  5: 0.096677,
                  6: 0.093375,
                  7: 0.090352,
                  8: 0.08757000000000001,
                  9: 0.084997},
                 'Counts': {0: 4318,
                  1: 2323,
                  2: 1348,
                  3: 1298,
                  4: 3060,
                  5: 6580,
                  6: 10092,
                  7: 9847,
                  8: 8439,
                  9: 6635},
                 'Found': {0: 0.080052,
                  1: 0.043066,
                  2: 0.024991,
                  3: 0.024064,
                  4: 0.056729999999999996,
                  5: 0.12198699999999998,
                  6: 0.187097,
                  7: 0.182555,
                  8: 0.156452,
                  9: 0.12300699999999999}})

fig = px.scatter(
    df,
    x="Found",
    y="Expected",
    trendline="ols",
    trendline_scope = 'overall',
    custom_data=['Expected','Found','Counts']
)

fig.update_traces(
    hovertemplate="<br>".join([
        "expected: %{customdata[0]}",
        "found: %{customdata[1]}",
        "counts: %{customdata[2]}"
        "<extra></extra>"
    ]))
fig.show()
1 Answers

The intent of the question is to answer with the understanding that you want the scatterplot and trendline hover to have different content. Since the data is presented but the trendline information is not, I will use the example of a hover with the torrent line information in the reference. Since this type of graph consists of a scatter plot and a line chart, there is a hover template for each, so changing the scatter plot side will change the hover for the scatter plot without changing the hover for the trendlines.

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df,
                 x="total_bill",
                 y="tip", 
                 trendline="ols",
                 trendline_scope='overall',
                 custom_data=['total_bill','tip','sex']
                )

fig.data[0]['hovertemplate'] = "total_bill: %{customdata[0]}<br>" + "tip: %{customdata[1]}<br>" + "sex: %{customdata[2]}<br>" + "<extra></extra>"

fig.show()

enter image description here

Before correcting the hover on the scatterplot

enter image description here

Related