I wanted to specify the colors for my individual traces using the following code:
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
df = districts.drop(['COUNTY', 'STATUS', 'TOTAL'], axis=1)
# 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')
However, the colors I want are only showing up in the first district as opposed to all 27 districts as seen here.