将 table_fig 中大于 80 的值的字体颜色更改为蓝色

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

我正在执行以下代码来绘制 table_fig 中大于 80 的值颜色。但是,它没有相应地将字体颜色设置为蓝色。例如,它正在为值 XPTB 设置 MVS_SYSTEM_ID 列的字体颜色,但不应这样做,因为它只绘制 MAX_CSA_USED%、MAX_ECSA_USED%、MAX_SQA_USED%、MAX_ESQA_USED% 列。此外,它的绘画/着色值字体远低于 80,例如 34.17、46.38、33.06 和 77.12。

有人可以帮助我吗?我真的很感谢您的帮助。

这是我的代码:

import pandas as pd
import plotly.graph_objs as go

# Criar um DataFrame com os dados fornecidos
data = {
    'SYSTEM_ID': ['XPTO', 'XPTO', 'XPTO', 'XPTX', 'XPTX', 'THE_FIRM', 'THE_FIRM', 'THE_FIRM'],
    'MVS_SYSTEM_ID': ['XPTO', 'XPTB', 'XRNA', 'ZZZZ', 'CCCC', 'NABD', 'XXXX', 'YYYY'],
    'MAX_CSA_USED%': [60.47, 71.41, 70.93, 35.14, 32.26, 24.1, 31.56, 22.81],
    'MAX_ECSA_USED%': [51.46, 81.9, 53.8, 35.02, 34.87, 37.47, 40.88, 14.17],
    'MAX_SQA_USED%': [42.52, 33.64, 42.23, 45.05, 40.61, 33.7, 25.39, 29.67],
    'MAX_ESQA_USED%': [56.26, 53.55, 57.13, 34.17, 46.38, 91.31, 33.06, 77.12]
}

table_df = pd.DataFrame(data)

# Definir as colunas do DataFrame
table_columns = ['SYSTEM_ID', 'MVS_SYSTEM_ID', 'MAX_CSA_USED%', 'MAX_ECSA_USED%', 'MAX_SQA_USED%', 'MAX_ESQA_USED%']

# Criar a figura da tabela
table_fig = go.Figure(data=[go.Table(
    header=dict(values=table_columns),
    cells=dict(values=[table_df[col] if col not in ['MVS_SYSTEM_ID', 'SYSTEM_ID'] else table_df[col].astype(str) for col in table_columns],
               align=['center', 'center', 'right'],
               font=dict(color=[['blue' if val > 80 else 'black' for val in row] for row in table_df[['MAX_CSA_USED%', 'MAX_ECSA_USED%', 'MAX_SQA_USED%', 'MAX_ESQA_USED%']].values]),
               fill=dict(color=[['lightblue' if val > 80 else 'white' for val in row] for row in table_df[['MAX_CSA_USED%', 'MAX_ECSA_USED%', 'MAX_SQA_USED%', 'MAX_ESQA_USED%']].values]),
))])

# Mostrar a tabela
table_fig.show()
python pandas plotly
1个回答
0
投票

查看代码,原因可能是字体颜色和填充颜色仅适用于数字列。可能还有其他方法可以做到这一点,但我创建了一个带有单独函数的列表,用于设置数据框值、字符串或其他颜色的颜色,如果是数字,则按条件设置颜色。而且绘图表是按列排列的,所以我将其更改为列规范。请查看参考示例了解详情

import pandas as pd
import plotly.graph_objs as go
import numpy as np

table_fig = go.Figure()

font_list = []
fill_list = []
for col in table_df.values.T.tolist():
    for r in col:
        if type(r) is str:
            font_list.append('black')
            fill_list.append('white')
        else:
            if r > 80.0:
                font_list.append('blue')
                fill_list.append('lightblue')
            else:
                font_list.append('black')
                fill_list.append('white')

table_fig.add_trace(go.Table(header=dict(values=table_df.columns),
                             cells=dict(values=table_df.values.T,
                                            align=['center','center','right','right','right','right'],
                                            font_color=np.array(font_list).reshape(6,8),
                                            fill_color=np.array(fill_list).reshape(6,8))
                                )
                        )

table_fig.update_layout(height=500)
table_fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.