I'm trying to recreate this Seaborn plot in Plotly: seaborn plot.
My dataframe has np.nan values, and in Plotly the trendlines turn out wacky: plotly plot. For example, the red scatterplot shows the sentiment of a character who appears later on in the TV show (thus, for the first few episodes, the values in the dataframe are np.nan). If you look just at the red trend line , you can see that it doesn't align with the red scatterplot.
If I take out the np.nan values then the problem disappears, but that's not what I want to do since I want the sentiments to align with each episode on the x-axis.
How can I fix this? Here's my code:
import plotly.express as px
fig = px.scatter(df, x='episode', y='sentiment', color='character', trendline="lowess")
fig.show()
EDIT:
I was offered a solution on the Plotly forum:
# 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()
This should be fixed in the next version of plotly.py.
The solution:
import plotly.express as px
fig = px.scatter(newdf, x='episode', y='sentiment', color='character', 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()
This should be fixed in the next version of plotly.py.