Hover Template Showing Previous Value Instead of NaN in Plotly

example:

data = {x:[1,2,3,4,5,6] y :[1,np.nan, 2, 3, np.nan,6}

for col in data.columns:
    for option in selected_options:
        if option in col:
             col_name = (col.replace("_", " ") .title())
                  fig.add_trace(
                               go.Scatter(
                                x=data.index,
                                y=data[col],
                                mode="lines+markers",
                                name=col_name,
                                hovertemplate=f"{col_name}: %{{y}}<extra></extra>",
                                showlegend=True,
                            )
                        )

Expected vs. Actual Behavior

  • Expected: When hovering over a NaN value, the hover tooltip should either display "NaN" or not appear at all.
  • Actual: Instead of showing NaN, the hover template incorrectly displays the previous valid value.

Screenshot of Issue

For x = 13, some columns don’t have values, but the hover template shows the previous index’s value instead of being empty or showing NaN.
(Screenshot attached)

Question

How can I modify my hover template to correctly handle NaN values by either showing "NaN" or nothing, instead of the previous value?

Any suggestions would be appreciated! Thanks. :blush: