Handling np.nan in Plotly trendline

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.

enter image description hereIf 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.

2

2 Answers

you can add this to the 'fig' element:

fig.update_traces(connectgaps=True)

it work for me.

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like