[英]Change the text color of cells in Plotly table based on value (string)
I have the following script that turns a pandas dataframe into an interactive html table with Plotly:我有以下脚本将 pandas dataframe 变成带有 ZA5E13387C005FE84D14E 的交互式 html 表
goals_output_df = pd.read_csv(os.path.join(question_dir, "data/output.csv"))
fig = go.Figure(data=[go.Table(
columnwidth = [15,170,15,35,35],
header=dict(values=['<b>' + x + '</b>' for x in list(goals_output_df.columns)],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=[goals_output_df[column] for column in goals_output_df.columns],
# fill_color='#e6f2fd',
line_color='darkslategray',
align='left',
font=dict(color='black', family="Lato", size=20),
height=30
))
])
fig.update_layout(
title="<b>Output summary for %s</b>"%question.strip('question'),
font=dict(
family="Lato",
size=18,
color="#000000"))
fig.write_html(os.path.join(question_dir, "results/output.html"))
The table contains a column named "Output" which can have one of three values for each row: "YES", "NO" and "BORDERLINE".该表包含一个名为“Output”的列,每行可以有以下三个值之一:“YES”、“NO”和“BORDERLINE”。
I want to be able to change the color of the text in the "Output" column such that "YES" is green, "NO" is red and "BORDERLINE" is blue.我希望能够更改“输出”列中文本的颜色,使“YES”为绿色,“NO”为红色,“BORDERLINE”为蓝色。
Any idea how I could do that?知道我该怎么做吗? Plotly documentation doesn't seem to help.
Plotly 文档似乎没有帮助。
It's kind of implicit on this section cell-color-based-on-variable .这部分隐含在cell-color-based-on-variable中。 Here what I'll do in your case.
这就是我将在你的情况下做的事情。
import pandas as pd
import plotly.graph_objects as go
df = pd.DataFrame({"name":["a", "b", "c", "d"],
"value":[100,20,30,40],
"output":["YES", "NO", "BORDERLINE", "NO"]})
map_color = {"YES":"green", "NO":"red", "BORDERLINE":"blue"}
df["color"] = df["output"].map(map_color)
cols_to_show = ["name", "value", "output"]
Where cols_to_show
are the only columns you want to show in your table.其中
cols_to_show
是您要在表中显示的唯一列。
Here you want to have a standard cell background in all columns but the output
one在这里,您希望在除
output
的所有列中都有标准单元格背景
fill_color = []
n = len(df)
for col in cols_to_show:
if col!='output':
fill_color.append(['#e6f2fd']*n)
else:
fill_color.append(df["color"].to_list())
data=[go.Table(
# columnwidth = [15,20,30],
header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=df[cols_to_show].values.T,
fill_color=fill_color,
line_color='darkslategray',
align='left',
font=dict(color='black', family="Lato", size=20),
height=30
))
]
fig = go.Figure(data=data)
fig.show()
UPDATE更新
In case you want to change text color如果您想更改文本颜色
text_color = []
n = len(df)
for col in cols_to_show:
if col!='output':
text_color.append(["black"] * n)
else:
text_color.append(df["color"].to_list())
and和
data=[go.Table(
# columnwidth = [15,20,30],
header=dict(values=[f"<b>{col}</b>" for col in cols_to_show],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=df[cols_to_show].values.T,
line_color='darkslategray',
align='left',
font=dict(color=text_color, family="Lato", size=20),
height=30
))
]
fig = go.Figure(data=data)
fig.show()
You could add an if statement in the font_color section:您可以在 font_color 部分添加 if 语句:
import pandas as pd
import plotly
import plotly.graph_objs as go
df = pd.DataFrame({"name":["a", "b", "c", "d"],
"value":[100,20,30,40],
"output":["YES", "NO", "BORDERLINE", "NO"]})
fig = go.Figure(data=[go.Table(
columnwidth = [30,30,30],
header=dict(values=["Name","Value", "Output"],
# fill_color='#b9e2ff',
line_color='darkslategray',
align='center',
font=dict(color='black', family="Lato", size=20),
height=30
),
cells=dict(values=[df.name, df.value, df.output],
# fill_color='#e6f2fd',
line_color='darkslategray',
align='left',
font_color=['darkslategray','darkslategray',['green' if x == "YES" else
"red" if x == "NO" else "blue" if x == "BORDERLINE" else 'darkslategray'
for x in list(df.output)]],
height=30
))
])
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.