首页 > 解决方案 > 指定的跟踪颜色未循环通过整个 Dash 图表

问题描述

我有一个看起来像这样的数据框,其中我正在绘制 2014-2018 年间纽约 27 个地区的政党的选民登记:

在此处输入图像描述

使用 Dash,我想使用以下代码为我的各个跟踪指定颜色:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd

# Read in the data 

districts = pd.read_csv("https://github.com/thedatasleuth/New-York-Congressional-Districts/blob/master/districts.csv?raw=True")

df = districts

# Get a list of all the years
years = districts['Year'].unique()

# Create the app
app = dash.Dash()

# Populate the layout with HTML and graph components
app.layout = html.Div([
    html.H2("New York Congressional Districts"),
    html.Div(
        [
            dcc.Dropdown(
                id="Year",
                options=[{
                    'label': i,
                    'value': i
                } for i in years],
                value='All Years'),
        ],
        style={'width': '25%',
               'display': 'inline-block'}),
    dcc.Graph(id='funnel-graph'),
])


# Add the callbacks to support the interactive componets
@app.callback(
    dash.dependencies.Output('funnel-graph', 'figure'),
    [dash.dependencies.Input('Year', 'value')])
def update_graph(Year):
    if Year == "All Years":
        df_plot = df.copy()
    else:
        df_plot = df[df['Year'] == Year]

    trace1 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('DEM')], name='DEM', 
                    marker=dict(color=['rgb(3,67,223)']))
    trace2 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('REP')], name='REP',
                    marker=dict(color=['rgb(229,0,0)']))
    trace3 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('CON')], name='CON',
                    marker=dict(color=['rgb(132,0,0)']))
    trace4 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('WOR')], name='WOR',
                    marker=dict(color=['rgb(149,208,252)']))
    trace5 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('IND')], name='IND',
                    marker=dict(color=['rgb(126,30,156)']))
    trace6 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('GRE')], name='GRE',
                    marker=dict(color=['rgb(21,176,26)']))
    trace7 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('WEP')], name='WEP',
                    marker=dict(color=['rgb(255,129,192)']))
    trace8 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('REF')], name='REF',
                    marker=dict(color=['rgb(206,162,253)']))
    trace9 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('OTH')], name='OTH',
                    marker=dict(color=['rgb(249,115,6)']))
    trace10 = go.Bar(x=df_plot ['DISTRICT'], y=df_plot [('BLANK')], name='BLANK',
                    marker=dict(color=['rgb(101,55,0)']))


    return {
        'data': [trace1, trace2, trace3, trace4, trace5, 
                     trace6, trace7, trace8, trace9, trace10],
        'layout':
        go.Layout(
            title='{}'.format(Year),
            barmode='group')
    }


if __name__ == '__main__':
    app.server.run(port=8000, host='127.0.0.1')

然而,我想要的颜色只出现在第一个区,而不是这里看到的所有 27 个区。

标签: pythonpandasplotly-dash

解决方案


我像这样从列表中删除了颜色:

marker=dict(color='rgb(3,67,223)'))


推荐阅读