Adding Hline to px.scatter with one scatter point

I have a scatter plot with one data point (for now) and I tried adding a hline with the same value as the data point on the chart and got the below:

I expected the line to cross through the point. if I change the value of the hline to a different value, the plot works as expected. Below is the code snippet as part of a Dash callback:

fig = px.scatter(df_final.loc[df_final['Fund'].isin([fund])], x='Trade Date', y="Points", size='Quantity',
                     hover_name='Dealer',
                     height=600, width=1500, color='Dealer',
                     hover_data={'Dealer': False, 'Quantity': False, 'Quantity (USD)': True,
                                 "Trade Date": "|%b %d, %Y"})

    fig.update_layout(title_text=f"{ccy} Roll ", title_x=0.5)

    fig.update_xaxes(
        rangebreaks=[
            dict(bounds=["sat", "mon"]),
            dict(values=dt_breaks)  # hide weekends
        ],
        tickformat="%b %d", dtick="D1"

    )
    
    fig.update_yaxes(tickformat=".2f")
    
    fig.add_hline(y=-5.25, line_dash='dash', line_color='red', annotation_text="labc",
                  annotation_position="bottom right")

    return fig





















































































































































































Hi @nickmuchi,

I would guess you observe this due to precision issues. Are you sure, the exact coordinate is -5.25? You might check this or fix the y- axis range to fig.update(layout_yaxis_range = [-10,0])

import plotly.express as px

fig = px.scatter(x=[10], y=[50])

fig.add_hline(y=50.001, line_dash='dash', line_color='red', annotation_text="labc",
                  annotation_position="bottom right")

Hi @AIMPED, yes the exact value is -5.25, I tried -5.20 and that worked as expected. It only does not work when the hline value is equivalent to the y-axis value of the scatter point. I was passing a variable into the y arg of add_hline and I made sure I had rounded it off to 2dp as well.

Strange indeed. Which values are shown on the y- axis if you increase the decimals, let’s say to fig.update_yaxes(tickformat=".10f")?

ye they are all zeros

If I update the y-axis range as you suggested it works:

However, my chart is dynamic so I will be extracting new values each day and adding scatter points which might have values outside of the range.

I think if you plot more than one point, you will not observe this behavior.

It’s not a very satisfying answer, but I have no idea what is happening and unfortunately with your given code snippet I can’t reproduce your problem.

Yes you are right, I added a second point and looks better, thanks for taking the time! much appreciated.