plotly / plotly/dash

Graph.figure.plot_bgcolor not updated with callback

Open
#1,107 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

Describe your context
My environment

dash                          1.8.0
dash-core-components          1.7.0
dash-html-components          1.0.2
dash-renderer                 1.2.3
dash-table                    4.6.0

My setup

- OS:  Windows 10
- Browser Firefox
- Version 72.0.2 (64-bit)

Describe the bug

Toggling between 2d and 3d scatter does not update plot_bgcolor as expected.

Expected behavior

When I am plotting 3d first, plot_bgcolor is set correctly. After switching to 2d and back to 3d, the plot_bgcolor of the 2d plot persists instead of getting overwritten by the plot_bgcolor of 3d.

Screenshots

Here is some code that replicates the error:

from itertools import product
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)



df = pd.DataFrame(product((1, -1), repeat=3), columns=["x", "y", "z"])
options = [{"value": f"{n}D", "label": f"{n}D"} for n in [2, 3]]
app.layout = html.Div([dcc.Graph(id='hypercube-plot', style={"width": "80vw", "height": "80vh"}),
                       html.Div(className='row', 
                                children=[html.Div([dcc.RadioItems(id="toggle-dims", options=options,
                                                                   value="3D")],
                                className='three columns')]
             )])


@app.callback(
    Output('hypercube-plot', 'figure'),
    [Input('toggle-dims', 'value')])
def display_hover_data(n_dim):
    if not n_dim:
        return {}
    try:
        n_dim = int(n_dim[0])
    except (ValueError, TypeError):
        return {}
    layout = dict(paper_bgcolor='#7f7f7f',
                  plot_bgcolor='#262626',)
    if n_dim == 2:
        data = [dict(type="scatter",
                     mode="markers",
                    x=df["x"],
                    y=df["y"],
                    color="white")]
        return {"data": data, "layout": layout}
    if n_dim != 3:
        return {}
    layout["plot_bgcolor"]='#7f7f7f'
    layout["scene"]=dict(xaxis={'title': {"backgroundcolor": "#262626"}},
                         yaxis={'title': {"backgroundcolor": "#262626"}},
                         zaxis={'title': {"backgroundcolor": "#262626"}})
    data = [dict(type="scatter3d",
                 mode="markers",
                x=df["x"],
                y=df["y"],
                z=df["z"],
                color="white")]
    return {"data": data, "layout": layout}

if __name__ == "__main__":
    app.run_server(debug=True)

I am attaching three screenshots too, where I toggle the dimensions.
1
2
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Dash example and tracing the dcc.Graph figure update from the display_hover_data callback when toggling between 2D and 3D. Check the graph rendering path for plot_bgcolor handling across those transitions. Done means switching 3D → 2D → 3D restores the 3D plot background color without regressing the 2D view.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.