绘图,点击后图例点不会消失

问题描述 投票:0回答:1
我正在尝试使用plotly制作散点图,它按预期表示2组数据(正常,异常)。问题出在图例点内,单击“正常”图例确实可以从图中正确过滤出“正常”数据点,另一方面单击“异常”不会将其过滤掉,而只是将图上的数据点变灰情节。

我不知道是什么触发了这种情况,但我希望该图能够正确地从图例中过滤掉数据点:

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()
    
python plotly visualization scatter-plot
1个回答
0
投票
当您将

px.scatter

color='Type'
 参数一起使用时,您已经添加了正常点和异常点。您不需要仅添加另一条仅异常点的痕迹,因此您可以摆脱该线

fig.add_trace(px.scatter(...))
以下内容应按预期工作:

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()
    
© www.soinside.com 2019 - 2024. All rights reserved.