Reputation:
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)
I want to have legends shown by the side of this plotly chart. Therefore, people can understand what the color of the points stand for. I cannot find a proper solution anywhere.
Upvotes: 6
Views: 9151
Reputation: 581
I think the issue here is two fold, first your PriceLevel is an integer so the plot is using it as a scale, which is why you specified showscale=False
. If you change that to showscale=True
you will get a legend of sorts but it will be a scale and you want points.
That's the second part; if you want to show them as individual scales you will have to set them up as individual traces.
For Example
# Set up a trace for each level of PriceLevel
trace1 = go.Scatter(
x=x_pca_df.query(" PriceLevel==1")['Principle'],
y=x_pca_df.query(" PriceLevel==1")['Second'],
# Add a name for each trace to appear in the legend
name = 'PriceLevel 1',
mode='markers',
marker=dict(color='rgba(152, 0, 0, .8)', size=4, showscale=False))
trace2 = go.Scatter(
x=x_pca_df.query(" PriceLevel==2")['Principle'],
y=x_pca_df.query(" PriceLevel==2")['Second'],
name = 'PriceLevel 2',
mode='markers',
marker=dict(color='rgba(255, 182, 193, .9)', size=4, showscale=False))
# Join them all together
data = [trace1, trace2]
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)
Hopefully that'll work.
Upvotes: 4
Reputation: 2998
You need to specify parameter name in your trace
. As I think from seeing the plot, you have four traces
. So you need to select name
for each trace
and choose what you want to see for each of them.
Code:
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')
Upvotes: 0
Reputation: 472
I think you need to specify filename parameter. Try this and let me know if it works.
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')
Upvotes: 0