Python可视化交互库——dash——设置颜色

  • 时间:2025-11-10 18:04 作者: 来源: 阅读:0
  • 扫一扫,手机访问
摘要:模块 dash.html 包含所有 HTML 标签, 可以用其设置文本及背景颜色.模块 dash.dcc 包含交互的高级组件.代码import pandas as pd import plotly.express as px from dash import Dash, html, dcc app = Dash() # 初始化 Dash # 预设样式 colors = { &#

模块 dash.html 包含所有 HTML 标签, 可以用其设置文本及背景颜色.

模块 dash.dcc 包含交互的高级组件.

  1. 代码
import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc

app = Dash()     # 初始化 Dash

# 预设样式
colors = {
    'background': '#7fffec',     # 背景颜色
    'text':       '#7FDBFF'      # 文字颜色
}

df = pd.DataFrame({'x': [1, 2, 3], 'SF': [4, 1, 2], 'Montreal': [2, 4, 5]})  # 原始数据: 'SF'和'Montreal'
fig = px.bar(df, x='x', y=['SF', 'Montreal'], barmode='group')                # 柱状图

# 更新柱状图样式: 设置背景颜色及文本颜色
fig.update_layout(
    plot_bgcolor=colors['background'],
    paper_bgcolor=colors['background'],
    font_color=colors['text']
)

app.layout = html.Div(
    style={'backgroundColor': colors['background']},    # 设置全局样式
    children=[
        html.H1(
            children='Hello Dash',
            style={
                'textAlign': 'center',                 # 文本对齐方式
                'color': colors['text']                # 文本内容
            }
        ),

        html.Div(
            children='Dash: 一款Python web应用框架',
            style={
                'textAlign': 'center',                 # 文本对齐方式
                'color': colors['text']                # 文本内容
            }
        ),

        dcc.Graph(
            id='dash-example-graph-colored',
            figure=fig
        )
    ])

if __name__ == '__main__':
    app.run_server(debug=True)
  1. 结果


Python可视化交互库——dash——设置颜色

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】MySQL系列—编译安装8.0版本时gcc及cmake版本说明(2025-11-10 18:06)
【系统环境|】调整随机森林超参数的可视化指南(2025-11-10 18:05)
【系统环境|】机器学习「铁三角」:Numpy、Pandas、Matplotlib协同实战指南(2025-11-10 18:05)
【系统环境|】在XGBoost和LightGBM模型中强制执行单调约束的python教程(2025-11-10 18:04)
【系统环境|】Python可视化交互库——dash——设置颜色(2025-11-10 18:04)
【系统环境|】MySQL 5.5 安装教程(附安装包下载)(2025-11-10 18:03)
【系统环境|】centos系统下安装MySQL5.7.18方法详解(2025-11-10 18:03)
【系统环境|】Windows安装Mysql及卸载(2025-11-10 18:02)
【系统环境|】MySQL安装(详细,适合小白)(2025-11-10 18:02)
【系统环境|】程序员必备技能——解压版MySQL安装(2025-11-10 18:01)
手机二维码手机访问领取大礼包
返回顶部