Plotly Dash - 如何设置自动颜色而不是为所有颜色设置蓝色

Plotly Dash - how to set automated colors rather than having blue for everything

我希望获得有关在 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'
        }
    })))

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

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

对于 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'
        }
    })))