【问题标题】:How to change labels of cureves in plotly.express.bar如何更改 plotly.express.bar 中的曲线标签
【发布时间】:2021-06-21 05:17:10
【问题描述】:

我有下图: 我需要更改图例中的标签。

我阅读了plotly.express.bar 的文档,我认为color_discrete_map 是相关的,但我不知道如何应用它。 如您所见,图中有 6 种颜色,我想为每种颜色分配一个文本。

def update_figure_profits(df0,
                          label,
                          xlabel=None,
                          ylabel=None,
                          title=None,
                          percent_fmt=False):
    c = ['#636EFA', '#EF553B', '#00CC96',
         '#AB63FA', '#FFA15A', '#19D3F3'] * 3
    df = df0[[label, 'name']].copy()
    fig = px.bar(df,
                 x=range(len(df)),
                 y=label,
                 color=c,
                 )
    fig.update_layout(xaxis=dict(
        tickmode='array',
        tickvals=list(range(18)),
        ticktext=['', '', 'کامل', '', '', ''] + ['', '', 'متغیر', '', '', ''] + 
        ['', '', 'فرامتغیر', '', '', '']
    )
    )
    fig.update_layout(xaxis_title=xlabel,
                      yaxis_title=ylabel,)
    fig.update_layout(title_text=title, title_x=0.5)
    return fig

数据看起来像这样:

      id         name          cost       sell        profit  profit_margin  efficiency_on_fixed_asset   costing_type
0   1000     سنگِ آهن  4.794900e+06   13759200  8.964300e+06       0.651513                   0.664022           full
1   2000     کنسانتره  1.155342e+07   22464000  1.091058e+07       0.485692                   0.808191           full
2   3000        گندله  1.470069e+07   32993999  1.829331e+07       0.554443                   1.355060           full
3   4000  آهنِ اسفنجی  2.669225e+07   70200000  4.350775e+07       0.619768                   3.222796           full
4   5000          شمش  5.577847e+07  140400000  8.462153e+07       0.602717                   6.268262           full
5   6000    نوردِ گرم  6.269642e+07  155984400  9.328798e+07       0.598060                   6.910220           full
6   1000     سنگِ آهن  3.863700e+06   13759200  9.895500e+06       0.719192                   0.733000       variable
7   2000     کنسانتره  8.976960e+06   22464000  1.348704e+07       0.600385                   0.999040       variable
8   3000        گندله  1.097908e+07   32993999  2.201492e+07       0.667240                   1.630735       variable
9   4000  آهنِ اسفنجی  1.932668e+07   70200000  5.087332e+07       0.724691                   3.768394       variable
10  5000          شمش  3.871631e+07  140400000  1.016837e+08       0.724243                   7.532125       variable
11  6000    نوردِ گرم  4.260937e+07  155984400  1.133750e+08       0.726836                   8.398151       variable
12  1000     سنگِ آهن  1.123200e+06   13759200  1.263600e+07       0.918367                   0.936000  supervariable
13  2000     کنسانتره  4.164883e+06   22464000  1.829912e+07       0.814597                   1.355490  supervariable
14  3000        گندله  5.850284e+06   32993999  2.714371e+07       0.822686                   2.010646  supervariable
15  4000  آهنِ اسفنجی  1.052448e+07   70200000  5.967552e+07       0.850079                   4.420409  supervariable
16  5000          شمش  2.459279e+07  140400000  1.158072e+08       0.824838                   8.578312  supervariable
17  6000    نوردِ گرم  2.535339e+07  155984400  1.306310e+08       0.837462                   9.676371  supervariable

感谢您的指导。

【问题讨论】:

    标签: plotly-python


    【解决方案1】:

    对不起,我没有阅读整个代码,但你在第二行所做的事情是错误的,bar 函数中的颜色参数并不意味着你写的颜色本身, 从 plotly 网站查看定义

    color (str or int or Series or array-like) – data_frame 中列的名称,或者 pandas Series 或 array_like 对象。此列或 array_like 中的值用于为标记分配颜色

    所以在你的情况下,颜色将是“costing_type”或“name”

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2016-02-05
      • 2020-10-21
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      相关资源
      最近更新 更多