Python Plotly数据帧中基于值的单元格颜色

2024-06-26 18:15:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pycharm。我想根据该值更改单元格的颜色。我正在使用这段代码,但是这是一个固定的颜色,它给我的输出如图enter image description here所示

import plotly.graph_objects as go
from plotly.colors import n_colors
import numpy as np
import pandas as pd

# np.random.seed(1)


df = pd.read_csv("IPLData.csv")
df = df.astype({"Matches": int, "Inns": int})
#
colors = n_colors(
    "rgb(255, 200, 200)", "rgb(200, 0, 0)", 14, colortype="rgb"
)
a = df["Matches"]
b = df["Inns"]
c = df["Runs"]
# b = np.random.randint(low=0, high=9, size=10)
# c = np.random.randint(low=0, high=9, size=10)

fig = go.Figure(
    data=[
        go.Table(
            header=dict(
                values=list(df.columns),
                line_color="white",
                fill_color="white",
                align="left",
            ),
            cells=dict(
                values=[df.Matches, df.Inns, df.Runs],
                fill_color="lavender",
                align="left",
            ),
        )
    ]
)

fig.show()

期望输出

enter image description here


Tags: csvimportgodf颜色asnprandom