Plotly trendlines not displaying correctly due to np.nan values

Hi @marichevy welcome to the forum! Thank you for the bug report, there is indeed a bug in the sense that x and y values are not properly aligned. I have opened a pull request to fix the issue (https://github.com/plotly/plotly.py/pull/2357), this should be fixed in the next version of plotly.py. In the meantime, you can manually correct the position of x points as follows:

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("continent == 'Oceania'")
df['pop'][df['year'] < 1970] = np.nan
fig = px.scatter(df, x='year', y='pop', color='country', trendline='lowess')
# Correct position of x points
for scatter, trendline in zip(fig.data[::2], fig.data[1::2]):
    trendline['x'] = scatter['x'][np.logical_not(np.isnan(scatter['y']))]
fig.show()
1 Like