答案 0 :(得分: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')
答案 1 :(得分:0)
您需要在trace
中指定参数name。从我的情节来看,您有四个traces
。因此,您需要为每个name
选择trace
,然后为每个视图选择要查看的内容。
代码:
trace = go.Scatter(
x=x_pca_df['Principle'],
y=x_pca_df['Second'],
#Set parameter name to what you want to see in legend
name = 'PriceLevel',
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'),
# Do not forget specify showlegend as True
yaxis=dict(title='Second Principle Component'), showlegend = True)
fig = dict(data=data, layout=layout)
# Parameter filename just create a html file in your python script directory with name
iplot(fig, filename = 'show-legend')