自定义图例的顺序 [英] Customizing the order of legends in plotly

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

问题描述

我正在尝试自定义图例的顺序,同时以plotly,python的方式绘制堆积的条形图.

I am trying to customize the order of legends while plotting stacked bar plots in plotly,python.

data = [
        go.Bar(
            y=df['sid'],  # assign x as the dataframe column 'x'
            x=df['A'],
            orientation='h',
            name='A'
        ),
        go.Bar(
            y=df['sid'],
            x=df['B'],
            orientation='h',
            name='B'
        ),

    ]

    layout = go.Layout(
        barmode='stack',
        title=f'{measurement}',
        xaxis=dict(
            title='Count',
            dtick=0),
        yaxis=dict(
            tickfont=dict(
                size=10,
            ),
            dtick=1)
    )

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

图例的顺序以相反的顺序显示(即从下到上).我想更改data中相应项目的从上到下的顺序.

The order of the legend appears in the reverse order(i.e from bottom to top). I want to change the order from top to bottom of the corresponding items in data.

我在此处看到了建议的选项Java的不确定如何在python中实现.

I saw the option suggested here for java. Not sure how to implement in python.

有人可以建议如何撤销订单吗?

Could someone suggest how the order can be reversed?

在生成的图像中,图例的顺序为

In the image that is generated the order of legend is

B
A

所需订单:

A
B

推荐答案

您可以使用图例的traceorder 键:

You can use traceorder key for legend:

确定图例项目的显示顺序.如果 正常",则项目的显示顺序从上到下与 输入数据.如果为反转",则项目以相反的方式显示 顺序为正常".如果是分组",则项目按组显示 (当提供跟踪legendgroup时).如果是分组+反转",则 项目以相反的顺序显示为分组".

Determines the order at which the legend items are displayed. If "normal", the items are displayed top-to-bottom in the same order as the input data. If "reversed", the items are displayed in the opposite order as "normal". If "grouped", the items are displayed in groups (when a trace legendgroup is provided). if "grouped+reversed", the items are displayed in the opposite order as "grouped".

在您的情况下,您应该修改layout定义:

In your case, you should modify your layout definition:

layout = go.Layout(
    barmode='stack',
    title=f'{measurement}',
    xaxis=dict(
        title='Count',
        dtick=0),
    yaxis=dict(
        tickfont=dict(
            size=10,
        ),
        dtick=1),
   legend={'traceorder':'normal'})
)


没有跟踪顺序说明

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

trace1 = go.Bar(x=['A', 'B', 'C'],
                y=[20, 14, 23],
                name='first')
trace2 = go.Bar(x=['A', 'B', 'C'],
                y=[12, 18, 29],
                name='second')

data = [trace1, trace2]
layout = go.Layout(barmode='stack',)

fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='stacked-bar')

具有跟踪顺序规范

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)

trace1 = go.Bar(x=['A', 'B', 'C'],
                y=[20, 14, 23],
                name='first')
trace2 = go.Bar(x=['A', 'B', 'C'],
                y=[12, 18, 29],
                name='second')

data = [trace1, trace2]
layout = go.Layout(barmode='stack',
                   legend={'traceorder':'normal'})

fig = go.Figure(data=data, layout=layout)
iplot(fig, filename='stacked-bar')

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

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