Hi all,
I recently came across a situation where I want to update the background colour of a graph.
I have a Graph
components and a Dropdown
that I use to update the figure
property of the Graph. Most of my figures have a gray background, however I need to change it to white for one of them.
The transition from the white to gray background works as expected, however, when I go from the gray background back to white, this doesn’t work.
I added a simplified code example below, as well as a gif showcasing the issue.
Has anyone come across this before? Is there a way to force the background to change colour?
Thanks!
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
app = dash.Dash()
app.layout = html.Div([
dcc.Dropdown(
id='example-dropdown',
options=[
{'label': 'Data 1', 'value': 'data-1'},
{'label': 'Data 2', 'value': 'data-2'},
],
value='data-1',
),
dcc.Graph(id='example-graph'),
])
@app.callback(dash.dependencies.Output('example-graph', 'figure'), [
dash.dependencies.Input('example-dropdown', 'value'),
])
def update_graph(value):
figure = go.Figure()
if value == 'data-1':
figure.add_traces(go.Sunburst(
ids=['North America', 'Europe', 'Australia', 'North America - Football', 'Soccer', 'North America - Rugby', 'Europe - Football', 'Rugby'],
labels=['North America', 'Europe', 'Australia', 'Football', 'Soccer', 'Rugby', 'Football', 'Rugby'],
parents=['', '', '', 'North America', 'North America', 'North America', 'Europe', 'Europe'],
values=[100, 100, 100, 33, 34, 33, 40, 20],
outsidetextfont={'size': 20, 'color': '#377eb8'},
leaf={'opacity': 0.4},
marker={'line': {'width': 2}},
))
figure.layout.plot_bgcolor = '#fff'
figure.layout.paper_bgcolor = '#fff'
elif value == 'data-2':
figure.add_traces([
{'x': [1, 2, 3], 'y': [2, 6, 2], 'type': 'bar', 'name': 'Set 1'},
{'x': [1, 2, 3], 'y': [5, 3, 8], 'type': 'bar', 'name': 'Set 2'},
])
figure.layout.plot_bgcolor = '#DCDCDC'
figure.layout.paper_bgcolor = '#fff'
return figure
if __name__ == '__main__':
app.run_server(debug=True)