情节:如何自定义图例? [英] Plotly: How to customize legend?

查看:55
本文介绍了情节:如何自定义图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经自定义了使用 plotly 绘制的数据点的颜色.数据点的颜色根据与之关联的标签进行分配.但是,在设置 legend = True 之后,所有三种颜色(在字典中定义)都不会显示在图中.

I have customized the color of data points plotted using 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.

我想要

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

显示在图的右上角.

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:

推荐答案

图例的外观和结构旨在反映图形的内容.你在这里想要完成的事情很有可能,但在我看来,最好以稍微不同的方式完成.如果您希望所有 a 标签的颜色为'rgb(147,112,219)',并且对 a 有多种观察,然后只需构建您的图形以准确显示该图形即可使图例有意义.通常,每条迹线只有一个图例元素.并且您有一个跟踪.您的方法与我的方法之间的主要区别在于,我为每个唯一的标签添加了一条痕迹.下面的代码段将为您提供以下图表,据我了解,这是您想要的结果:

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. 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:

完整代码:

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

这篇关于情节:如何自定义图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆