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

Python 在Plotly散点图中按颜色添加图例,python,plotly,Python,Plotly,我想让图例显示在这张图表的旁边。因此,人们可以理解点的颜色代表什么。我在任何地方都找不到合适的解决办法。 我认为您需要指定filename参数。试试这个,让我知道它是否有效 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 = [t

我想让图例显示在这张图表的旁边。因此,人们可以理解点的颜色代表什么。我在任何地方都找不到合适的解决办法。


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

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)

您需要在
跟踪中指定参数。正如我从图中看到的那样,您有四条
轨迹
。因此,您需要为每个
trace
选择
name
,并为每个
trace
选择希望看到的内容

代码:


我认为这里的问题有两个方面,首先,您的PriceLevel是一个整数,因此绘图使用它作为刻度,这就是您指定
showscale=False
的原因。如果您将其更改为
showscale=True
,您将得到一个排序图例,但它将是一个比例,您需要点数

这是第二部分;如果要将它们显示为单独的比例,则必须将它们设置为单独的轨迹

比如说

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')

希望这能奏效

我是根据你的建议做的,但正如你从我的帖子中看到的,它显示“跟踪0”而不是“价格级别”不起作用……size=4表示点大小,但不是4个子地块。我希望通过颜色(“价格级别”)显示图例。您的建议只能显示“跟踪0”的图例,但不能按点的颜色显示。您可以将
color
设置为
home\u data['PriceLevel']
。你能从这个专栏中得到一些数据吗,这个专栏是什么样子的?(例如,
print(home_data['PriceLevel']
)home_data['PriceLevel']=pd.qcut(home_data.SalePrice,[0,25,5,75,1],labels=[1,2,3,4])的输出按价格分类,我将它们分为四个部分。嗯,我可以建议为每个部分(四列)创建一列。然后创建四个跟踪(每列的每个跟踪)。根据需要命名列(如“first segment”等),但不要忘记在跟踪中设置参数名(如“0.25 segment”或类似的名称)。在第一道第一列、第二道-第二列等中也选择。然后,当您设置showlegend=True时,您应该会看到4个带名称的段,每个段的名称和颜色旁边都有您想出来的颜色吗?我也有类似的问题。
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')
# 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)