我正在处理散点图,并且正在努力为我的自定义数据点添加图例:
fig = px.scatter(df, x='Total Size', y='IFScores', hover_data={'Number of Rows': True, 'IFScores': True, 'Total Size': True, 'Adjusted Size': False}, size='Adjusted Size')
fig.add_trace(px.scatter(df[df['IFAnomaly'] == -1], x='Total 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(xaxis_title='Total Size', yaxis_title='Isolation Forest Scores')
fig.update_traces(marker=dict(sizemin=3))
fig.show()
我检查了官方文档,寻找有帮助的参数,但我无法使任何工作发挥作用。
要在图例中添加自定义数据点,请使用
plotly.graph_objs.Scatter
,它允许您在使用 name
创建散点图时添加
add_trace
参数
import plotly.graph_objs as go
fig = px.scatter(df, x='Total Size', y='IFScores', hover_data={'Number of Rows': True, 'IFScores': True, 'Total Size': True, 'Adjusted Size': False}, size='Adjusted Size')
fig.add_trace(
go.Scatter(
x=df[df['IFAnomaly'] == -1]['Total Size'],
y=df[df['IFAnomaly'] == -1]['IFScores'],
mode='markers',
marker=dict(color='red', sizemin=3, size=df[df['IFAnomaly'] == -1]['Adjusted Size']),
name='Anomaly'
)
)
fig.update_layout(xaxis_title='Total Size', yaxis_title='Isolation Forest Scores')
fig.show()
图例中出现的名称存储在
name='Anomaly'