Search code examples
pythonplotlyvisualizationscatter-plot

Plotly, legend points don't disappear after click


I'm trying to make a scatterplot using plotly, where there are 2 groups of data (normal, anomalous) being represented. The problem lays within the legend points, clicking 'normal' legend does filter the 'normal' data point out correctly from the plot, clicking 'anomalous' in the other hand doesn't filter it out but simply grays out the data points on the plot.

I don't know what is triggering the situation but I expect the plot the be able to filter out data points from the legend correctly:

df['Type'] = 'Normal'
df.loc[df['IFAnomaly'] < 0, 'Type'] = 'Anomalous'

fig = px.scatter(df, x='Raw Data Size', y='IFScores', hover_data={'Number of Rows': True, 'IFScores': True, 'Total Size': True, 'Adjusted Size': False}, color='Type', color_discrete_map={'Normal': 'green', 'Anomalous': 'red'}, size='Adjusted Size')
fig.add_trace(px.scatter(df[df['IFAnomaly']==-1], x='Raw Data Size', y='IFScores', hover_data={'Number of Rows': True, 'IFScores': True, 'Total Size': True, 'Adjusted Size': False},color_discrete_sequence=['red'], size='Adjusted Size').data[0])
fig.update_layout(title='Normal vs. Anomalous data points', xaxis_title='Raw Data Size', yaxis_title='Isolation Forest Scores', legend=dict(traceorder='reversed'))
fig.update_traces(marker=dict(sizemin=3))
fig.update_layout(title='Normal vs. Anomalous data points')
fig.show()

Solution

  • When you use px.scatter with the color='Type' argument, you are already adding both normal and anomalous points. You don't need to add another trace of only anomalous points, so you can get rid of the line

    fig.add_trace(px.scatter(...))
    

    The following should work as expected:

    df['Type'] = 'Normal'
    df.loc[df['IFAnomaly'] < 0, 'Type'] = 'Anomalous'
    
    fig = px.scatter(df, x='Raw Data Size', y='IFScores', hover_data={'Number of Rows': True, 'IFScores': True, 'Total Size': True, 'Adjusted Size': False}, color='Type', color_discrete_map={'Normal': 'green', 'Anomalous': 'red'}, size='Adjusted Size')
    fig.update_layout(title='Normal vs. Anomalous data points', xaxis_title='Raw Data Size', yaxis_title='Isolation Forest Scores', legend=dict(traceorder='reversed'))
    fig.update_traces(marker=dict(sizemin=3))
    fig.update_layout(title='Normal vs. Anomalous data points')
    fig.show()