简体   繁体   English

Plotly:如何自定义图例?

[英]Plotly: How to customize legend?

I have customized the color of data points plotted using plotly.我已经自定义了使用 plotly 绘制的数据点的颜色。 The color of the data points are assigned based on the label associated with it.数据点的颜色是根据与其关联的标签分配的。 However, after setting legend = True all three colors(defined in the dictionary) are not displayed in the plot.但是,在设置legend = True所有三种颜色(在字典中定义)都不会显示在图中。

I want,我想要,

'a': 'rgb(147,112,219)(the actual color in here)',
'b': 'rgb(220,20,60)',
'c': 'rgb(0,128,0)'

to be displayed in the top-right corner of the plot.显示在图的右上角。

import pandas as pd
import plotly  as plotly
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot


label = ['a', 'b', 'a', 'b', 'c']
label_df = pd.DataFrame({'color': label})

color = {'a': 'rgb(147,112,219)',
         'b': 'rgb(220,20,60)',
         'c': 'rgb(0,128,0)'
        }

cols = label_df['color'].map(color)

data = [
    go.Scatter(
        x=[1, 2, 3, 4, 5],
        y=[1, 2, 3, 4, 5],
        mode='markers',
        marker=dict(size=10, color=cols)
    )
]
layout = go.Layout(
    hovermode='y',
    showlegend=True,
    barmode='stack',
    title='plot'
)

fig = go.Figure(data=data, layout=layout)
plot(fig, filename='plot.html')

Any suggestion on how to display the customized legend in the plot?关于如何在图中显示自定义图例的任何建议?

Here is the figure produced by the code snippet:这是代码片段生成的图:

在此处输入图片说明

The look and structure of the legend is designed to reflect the content of your figure.图例的外观和结构旨在反映您的图形内容。 What you're aiming to accomplish here is very much possible, but in my opinion best done in a slightly different way.你在这里想要完成的事情很有可能,但在我看来,最好以稍微不同的方式完成。 If you'd like all a labels to have the color 'rgb(147,112,219)' , and you've got multiple observations of a , then just construct your figure to show exactly that in order to make the legend make sense.如果您希望所有a标签都具有颜色'rgb(147,112,219)' ,并且您对a了多次观察,那么只需构建您的图形以准确显示,以使图例有意义。 Generally, you'll have one legend element per trace.通常,每条轨迹都有一个图例元素。 And you've got one trace.你有一个痕迹。 The main difference between your and my approach is that I'm adding one trace per unique label.你和我的方法之间的主要区别是我为每个唯一标签添加一个跟踪。 The code snippet below will give you the following plot which I understand to be your desired result:下面的代码片段将为您提供以下情节,我认为这是您想要的结果:

在此处输入图片说明

Complete code:完整代码:

import pandas as pd
import plotly.graph_objects as go

df = pd.DataFrame({'label': ['a', 'b', 'a', 'b', 'c'],
                   'x': [1, 2, 3, 4, 5],
                   'y': [1, 2, 3, 4, 5]})

color = {'a': 'rgb(147,112,219)',
         'b': 'rgb(220,20,60)',
         'c': 'rgb(0,128,0)'}

fig = go.Figure()
for lbl in df['label'].unique():
    dfp = df[df['label']==lbl]
    #print(dfp)
    fig.add_traces(go.Scatter(x=dfp['x'], y=dfp['y'], mode='markers',
                             name=lbl,
                             marker = dict(color=color[lbl], size = 12)
                             ))

fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM