How to make page background match chart background

Hello. I just started using Dash. I am using Dash Bootstrap Components, and Dash Bootstrap Templates to have a coherent theme. However, it seems the page background color doesnt match with chart color

import dash
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
import plotly.express as px
from dash import html, dcc
import pandas as pd

PLOTLY_LOGO = "https://images.plot.ly/logo/new-branding/plotly-logomark.png"
load_figure_template("darkly")

df = pd.DataFrame(
    {
        "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
        "Amount": [4, 1, 2, 2, 4, 5],
        "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"],
    }
)

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")
fig2 = px.bar(df, y="Fruit", x="Amount", color="City", barmode="group")


def update_bg(fig):
    fig.update_xaxes(showgrid=False)
    fig.update_yaxes(showgrid=False)
    fig.update_layout({"plot_bgcolor": "rgba(0,0,0,0)"})


update_bg(fig)
update_bg(fig2)

app = dash.Dash(external_stylesheets=[dbc.themes.DARKLY])
logo = dbc.Navbar(
    dbc.Container(
        [
            html.A(
                # Use row and col to control vertical alignment of logo / brand
                dbc.Row(
                    [
                        dbc.Col(html.Img(src=PLOTLY_LOGO, height="75px")),
                        dbc.Col(
                            dbc.NavbarBrand(
                                [
                                    html.Span("Fruit ", style={"color": "#E03531"}),
                                    html.Span("Stand INC", style={"color": "#2c69b0"}),
                                ],
                                className="ms-2",
                            )
                        ),
                    ],
                    align="center",
                    className="g-0",
                ),
                href="/",
                style={"textDecoration": "none"},
            ),
        ],
        fluid=True,
    ),
    dark=True,
    color="dark",
    className="mb-5",
)


app.layout = html.Div(
    [
        logo,
        dbc.Container(
            [
                dbc.Row(
                    [dbc.Col(dcc.Graph(figure=fig)), dbc.Col(dcc.Graph(figure=fig2))]
                ),
                dbc.Row([dbc.Col(dcc.Graph(figure=fig2))]),
            ]
        ),
    ]
)


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

Hi @_deaconstjohn welcome to the forums.

Do you want the page color to match the figure or the figure to match the page color? In order to set the figure background to transparent, you could modify your def update_bg a bit:

fig.update_layout({"plot_bgcolor": "rgba(0,0,0,0)", "paper_bgcolor": 'rgba(0,0,0,0)'})

Thanks! I want the website background to match the chart background.

DBC themes such as the one you use DARKLY use a set of colors, you can take a look at those colors here:

https://dash-bootstrap-components.opensource.faculty.ai/docs/themes/explorer/

In your case, you are using the dark color as background for your container, you could use any other predefined color by using the corresponding classNames:

app.layout = html.Div(
    [
        logo,
        dbc.Container(
            [
                dbc.Row(
                    [dbc.Col(dcc.Graph(figure=fig)), dbc.Col(dcc.Graph(figure=fig2))]
                ),
                dbc.Row([dbc.Col(dcc.Graph(figure=fig2))]),
            ],
            className='bg-danger'
        ),
    ]
)

What you had to to is, either changing the predefined colors or use the style parameter to set the background to the color you would like to have.

app.layout = html.Div(
    [
        logo,
        dbc.Container(
            [
                dbc.Row(
                    [dbc.Col(dcc.Graph(figure=fig)), dbc.Col(dcc.Graph(figure=fig2))]
                ),
                dbc.Row([dbc.Col(dcc.Graph(figure=fig2))]),
            ],
        ),
    ],
    style={'background-color': 'rgb(50, 205, 50)'}
)

You could also use a custom css file for that.

Thanks @AIMPED ! I just need to find now what color the chart is using either by going to their source or any other tool :smiley:

1 Like

Hi @_deaconstjohn

To see the details of any of the templates in the dash-bootstrap-templates library:


from dash_bootstrap_templates import load_figure_template
import plotly.io as pio

load_figure_template("darkly")
plotly_template = pio.templates["darkly"]
print(plotly_template.layout)


This works for any Plotly Figure Template - for more info see the last example in this page:

Here’s the result:

Layout({
    'annotationdefaults': {'arrowcolor': '#f2f5fa', 'arrowhead': 0, 'arrowwidth': 1, 'font': {'color': '#fff'}},
    'autotypenumbers': 'strict',
    'coloraxis': {'colorbar': {'outlinewidth': 0, 'ticks': ''}},
    'colorscale': {'diverging': [[0, '#8e0152'], [0.1, '#c51b7d'], [0.2,
                                 '#de77ae'], [0.3, '#f1b6da'], [0.4, '#fde0ef'],
                                 [0.5, '#f7f7f7'], [0.6, '#e6f5d0'], [0.7,
                                 '#b8e186'], [0.8, '#7fbc41'], [0.9, '#4d9221'],
                                 [1, '#276419']],
                   'sequential': [[0.0, '#375a7f'], [0.1, '#4f5e7d'], [0.2,
                                  '#66627b'], [0.30000000000000004, '#7e677a'],
                                  [0.4, '#956b78'], [0.5, '#ad6f76'],
                                  [0.6000000000000001, '#c47374'],
                                  [0.7000000000000001, '#dc7872'], [0.8,
                                  '#f47c71'], [0.9, '#ff806f'], [1.0, '#ff846d']],
                   'sequentialminus': [[0.0, '#0d0887'], [0.1111111111111111,
                                       '#46039f'], [0.2222222222222222, '#7201a8'],
                                       [0.3333333333333333, '#9c179e'],
                                       [0.4444444444444444, '#bd3786'],
                                       [0.5555555555555556, '#d8576b'],
                                       [0.6666666666666666, '#ed7953'],
                                       [0.7777777777777778, '#fb9f3a'],
                                       [0.8888888888888888, '#fdca26'], [1.0,
                                       '#f0f921']]},
    'colorway': [#324c71, #b71e1d, #06a843, #ec9d0e, #6eaff5],
    'font': {'color': '#fff',
             'family': ('Lato,-apple-system,BlinkMacSys' ... 'oe UI Emoji","Segoe UI Symbol"')},
    'geo': {'bgcolor': '#222',
            'lakecolor': '#222',
            'landcolor': '#222',
            'showlakes': True,
            'showland': True,
            'subunitcolor': '#506784'},
    'hoverlabel': {'align': 'left',
                   'font': {'family': ('Lato,-apple-system,BlinkMacSys' ... 'oe UI Emoji","Segoe UI Symbol"')}},
    'hovermode': 'closest',
    'mapbox': {'style': 'dark'},
    'paper_bgcolor': '#303030',
    'piecolorway': [#324c71, #b71e1d, #06a843, #ec9d0e, #6eaff5],
    'plot_bgcolor': '#222',
    'polar': {'angularaxis': {'gridcolor': '#506784', 'linecolor': '#506784', 'ticks': ''},
              'bgcolor': 'rgb(17,17,17)',
              'radialaxis': {'gridcolor': '#506784', 'linecolor': '#506784', 'ticks': ''}},
    'scene': {'xaxis': {'backgroundcolor': 'rgb(17,17,17)',
                        'gridcolor': '#506784',
                        'gridwidth': 2,
                        'linecolor': '#506784',
                        'showbackground': True,
                        'ticks': '',
                        'zerolinecolor': '#C8D4E3'},
              'yaxis': {'backgroundcolor': 'rgb(17,17,17)',
                        'gridcolor': '#506784',
                        'gridwidth': 2,
                        'linecolor': '#506784',
                        'showbackground': True,
                        'ticks': '',
                        'zerolinecolor': '#C8D4E3'},
              'zaxis': {'backgroundcolor': 'rgb(17,17,17)',
                        'gridcolor': '#506784',
                        'gridwidth': 2,
                        'linecolor': '#506784',
                        'showbackground': True,
                        'ticks': '',
                        'zerolinecolor': '#C8D4E3'}},
    'shapedefaults': {'line': {'color': '#f2f5fa'}},
    'sliderdefaults': {'bgcolor': '#C8D4E3', 'bordercolor': 'rgb(17,17,17)', 'borderwidth': 1, 'tickwidth': 0},
    'ternary': {'aaxis': {'gridcolor': '#506784', 'linecolor': '#506784', 'ticks': ''},
                'baxis': {'gridcolor': '#506784', 'linecolor': '#506784', 'ticks': ''},
                'bgcolor': 'rgb(17,17,17)',
                'caxis': {'gridcolor': '#506784', 'linecolor': '#506784', 'ticks': ''}},
    'title': {'x': 0.05},
    'updatemenudefaults': {'bgcolor': '#506784', 'borderwidth': 0},
    'xaxis': {'automargin': True,
              'gridcolor': '#343434',
              'gridwidth': 0.5,
              'linecolor': '#506784',
              'ticks': '',
              'title': {'standoff': 15},
              'zerolinecolor': '#343434',
              'zerolinewidth': 2},
    'yaxis': {'automargin': True,
              'gridcolor': '#343434',
              'gridwidth': 0.5,
              'linecolor': '#506784',
              'ticks': '',
              'title': {'standoff': 15},
              'zerolinecolor': '#343434',
              'zerolinewidth': 2}
})


2 Likes

Thank you! :smiley:

1 Like