无法更新颜色栏标题 - 破折号

问题描述 投票:0回答:0

我似乎无法更新 Plotly colorbar 图上的标题。我尝试了下面列出的多种方法,但无法从 color.

更改它
  1. colorbar=dict(title='Colorbar Title Here')
  2. coloraxis_colorbar_title_text = "Colorbar Title Here"
  3. fig.data[0].colorbar.title = "Title Here"

安装的 Plotly 包是

5.11.0
.

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
import plotly.graph_objs as go
import pandas as pd
from datetime import datetime as dt
from dash.exceptions import PreventUpdate

df = pd.DataFrame({
       'Type': ['A','B','B','B','C','C','D','E','E','E','E','F','F','F'],
       })

N = 30
df = pd.concat([df] * N, ignore_index=True)

df['TIMESTAMP'] = pd.date_range(start='2022/01/01 07:30', end='2022/01/30 08:30', periods=len(df))
df['TIMESTAMP'] = pd.to_datetime(df['TIMESTAMP'], dayfirst = True).sort_values()

df['DATE'], df['TIME'] = zip(*[(d.date(), d.time()) for d in df['TIMESTAMP']])
df['DATE'] = pd.to_datetime(df['DATE'],format='%Y-%m-%d')

df = df.sort_values(by = 'DATE')

df['DOW'] = df['DATE'].dt.weekday
df = df.sort_values('DOW').reset_index(drop=True)
df['DOW'] = df['DATE'].dt.day_name()
df['DATE'] = df['DATE'].astype(str)
df['Color'] = df['DOW'].map(dict(zip(df['DOW'].unique(),
                px.colors.qualitative.Plotly[:len(df['DOW'].unique())])))

Color = df['Color'].unique()
Category = df['DOW'].unique()

cats = dict(zip(Category,Color)) 
external_stylesheets = [dbc.themes.SPACELAB, dbc.icons.BOOTSTRAP]

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

filter_box = html.Div(children=[

    html.Div(children=[
        html.Label('Date', style={'paddingTop': '2rem'}),
        dcc.Dropdown(
            id='DATE',
            options=[
                {'label': x, 'value': x} for x in df['DATE'].unique()
            ],
            value=df['DATE'].unique(),
            multi=True
        ),
     ], className="four columns",
    style={'padding':'2rem', 'margin':'1rem'} )
 ])

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col(html.Div(filter_box, className="bg-secondary h-100"), width=2),
        dbc.Col([
            dbc.Row([
                dbc.Col(dcc.Graph(id = 'date-bar-chart'), 
                    ),
            ]),
        ], width=5),
    ])
], fluid=True)

@app.callback(
    Output('date-bar-chart', 'figure'),
    [Input("DATE", "value"),
    ])     

def date_chart(date):

    if date:

        dff = df[df['DATE'].isin(date)]

        count = dff.groupby(['DATE',"DOW"])['DATE'].count().reset_index(name='counts')

        data = px.bar(x = count['DATE'], 
                      y = count['counts'],
                      color = count['DOW'],
                      color_discrete_map = cats,
                      #colorbar=dict(title='Colorbar Title Here') 
                      )

        layout = go.Layout(title = 'Date')
        fig = go.Figure(data = data, layout = layout) 

        fig.update_layout(coloraxis_colorbar_title_text = "Colorbar Title Here",
                          xaxis_title = 'Date', 
                          yaxis_title = 'Count',
                          margin = dict(l = 5, r = 5, t = 40, b = 5),
                          title_text = 'Title Here', title_x = 0.5, title_y = 0.96
                          );

        #fig.data[0].colorbar.title = "Title Here"
        fig.layout.coloraxis.colorbar.title = 'Colorbar Title Here'

        return fig
    else:
        raise PreventUpdate

if __name__ == '__main__':
    app.run_server(debug = True, port = 8051)

python plotly plotly-dash
© www.soinside.com 2019 - 2024. All rights reserved.