Search code examples
pythonjupyter-notebookplotlyplotly-pythonplotly-express

Unintended Additional line drawn by Plotly express in Python


plotly_with_extra_diagonal_line

Plotly draws an extra diagonal line from the start to the endpoint of the original line graph.

Other data, other graphs work fine.

Only this data adds the line.

Why does this happen?

How can I fix this?

Below is the code

temp = pd.DataFrame(df[{KEY_WORD}])
temp['date'] = temp.index
fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable')
fig.show()
plotly.offline.plot(fig,filename='Fig_en1')

Solution

  • A similar question has been asked and answered in the post How to disable trendline in plotly.express.line?, but in your case I'm pretty sure the problem lies in temp.melt(id_vars="date"), x='date', y='value', color='variable'. It seems you're transfomring your data from a wide to a long format. You're using color='variable' without specifying that in temp.melt(id_vars="date"). And when the color specification does not properly correspond to the structure of your dataset, an extra line like yours can occur. Just take a look at this:

    Command 1:

    fig = px.line(data_frame=df_long, x='Timestamp', y='value', color='stacked_values')
    

    Plot 1:

    enter image description here

    Command 2:

    fig = px.line(data_frame=df_long, x='Timestamp', y='value')
    

    Plot 2:

    enter image description here

    See the difference? That's why I think there's a mis-specification in your fig=px.line(temp.melt(id_vars="date"), x='date', y='value', color='variable').

    So please share your data, or a sample of your data that reproduces the problem, and I'll have a better chance of verifying your problem.