我不知道是什么触发了这种情况,但我希望该图能够正确地从图例中过滤掉数据点:
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()
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()