【问题标题】:Plotly Dash - how to set automated colors rather than having blue for everythingPlotly Dash - 如何设置自动颜色而不是为所有内容设置蓝色
【发布时间】:2018-05-29 08:33:44
【问题描述】:

我正在寻求帮助,以创建 Dash 条形图(绘图)并为所有条形图提供一种以上的颜色,目前它们都是蓝色的。

例如

    pv = pd.pivot_table(df, index=['customer_name2'], columns=['FY'], values=['amount'], aggfunc=sum, fill_value=0)
    pv2 = pv.reindex(pv['amount'].sort_values(by='FY17', ascending=True).index).head(x_bottom)
    trace1 = go.Bar(x=pv2.index, y=pv2[('amount','FY17')], name='Revenue')

    graphs.append(html.Div(dcc.Graph(
    id='chart_id_6',
    figure={
        'data': [trace1],
        'layout': {
            'title': 'Bottom ' + str(x_bottom) + ' Customers'
        }
    })))

【问题讨论】:

    标签: python plotly plotly-dash


    【解决方案1】:

    您可以传递一个颜色列表,每个 Bar 使用不同的颜色。

    trace1 = go.Bar(
        x=pv2.index, 
        y=pv2[('amount','FY17')], 
        name='Revenue',
        marker=dict(
            color=['rgba(204,204,204,1)', 
                   'rgba(222,45,38,0.8)',
                   ...])
    )
    

    参见https://plot.ly/python/bar-charts/ 章节:自定义条形颜色

    这也可能有帮助:Different colors for bars in barchart by their value

    【讨论】:

      【解决方案2】:

      对于json格式,下面的代码可以工作:

      graphs.append(html.Div(dcc.Graph(
          id='chart_id_6',
          figure={
              'data': [{'x':pv2.index, 'y': pv2[('amount','FY17')],
                        'marker': {'color': ['#b50000', '#e63535', '#fa8989']}}],
              'layout': {
                  'title': 'Bottom ' + str(x_bottom) + ' Customers'
              }
          })))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-09
        • 2014-11-15
        • 2021-12-26
        • 2011-06-13
        • 2020-12-14
        • 2014-10-01
        • 2020-12-04
        • 2020-05-08
        相关资源
        最近更新 更多