在 Plotly 散点图中按颜色添加图例

5 python plotly

trace = go.Scatter(
x=x_pca_df['Principle'],
y=x_pca_df['Second'],
mode='markers',
marker=dict(color=home_data['PriceLevel'], size=4, showscale=False))

data = [trace]

layout = dict(
title='PCA Analysis',
xaxis=dict(title='First Principle Component'),
yaxis=dict(title='Second Principle Component'))
fig = dict(data=data, layout=layout)
iplot(fig)
Run Code Online (Sandbox Code Playgroud)

我想在这个情节图的侧面显示图例。因此,人们可以理解点的颜色代表什么。我在任何地方都找不到合适的解决方案。 在此处输入图片说明

在此处输入图片说明

Dan*_*nny 0

我认为您需要指定文件名参数。试试这个,让我知道它是否有效。

trace = go.Scatter(
x=x_pca_df['Principle'],
y=x_pca_df['Second'],
mode='markers',
marker=dict(color=home_data['PriceLevel'], size=4, showscale=False))

data = [trace]

layout = dict(
title='PCA Analysis',
xaxis=dict(title='First Principle Component'),
yaxis=dict(title='Second Principle Component'), showlegend = True)
fig = dict(data=data, layout=layout)
iplot(fig, filename = 'show-legend')
Run Code Online (Sandbox Code Playgroud)