You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Dash图表的系列颜色?新手技术咨询

Customizing Series Colors in Your Dash Bar Chart

Hey there! Great job getting your user-bot engagement chart up and running on your site—customizing those default blue/orange colors is totally achievable, even if it’s not front-and-center in the docs. Let me break down two straightforward ways to set your own custom hues:

Option 1: Set Colors Per Individual Trace

If you want fine-grained control over each bar series, you can add a color parameter directly to each data trace in your figure. Just pick your preferred hex codes (or named colors like 'darkred') and plug them in:

dcc.Graph(
    id='average_engagement_graph',
    figure={
        'data': [
            {
                'x': list(avg_df['day']),
                'y': list(avg_df['countMessageIn']),
                'type': 'bar',
                'name': 'Incoming Messages from Users',
                'color': '#2ecc71'  # Custom green for incoming messages
            },
            {
                'x': list(avg_df['day']),
                'y': list(avg_df['countMessageOut']),
                'type': 'bar',
                'name': 'Outgoing Message by Bot',
                'color': '#3498db'  # Custom blue for bot messages
            },
        ],
        'layout': {
            'title': 'Average User-Bot Engagement by Day of Week',
            'xaxis': {
                'title': 'Day of the Week'
            },
            'yaxis': {
                'title': 'Average Number of Messages'
            },
        }
    }
),

Option 2: Use a Global Color Palette (Colorway)

If you plan to add more series later and want a consistent color scheme across all traces, use the colorway property in your layout. This lets you define a list of colors that Dash will cycle through for each new series:

dcc.Graph(
    id='average_engagement_graph',
    figure={
        'data': [
            {
                'x': list(avg_df['day']),
                'y': list(avg_df['countMessageIn']),
                'type': 'bar',
                'name': 'Incoming Messages from Users'
            },
            {
                'x': list(avg_df['day']),
                'y': list(avg_df['countMessageOut']),
                'type': 'bar',
                'name': 'Outgoing Message by Bot'
            },
        ],
        'layout': {
            'title': 'Average User-Bot Engagement by Day of Week',
            'xaxis': {
                'title': 'Day of the Week'
            },
            'yaxis': {
                'title': 'Average Number of Messages'
            },
            'colorway': ['#e74c3c', '#f39c12']  # Custom red and orange palette
        }
    }
),

Pro Tip

You can use any valid CSS color format here—hex codes, RGB/RGBA values, or even named colors like 'purple' or 'teal'. Just make sure the colors have enough contrast so your chart stays readable!

内容的提问来源于stack exchange,提问作者tansawit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:13:08