基于索引值的 Plotly table 颜色
Colour Plotly table based on value with index
我有一个数据框
如何将其绘制为 Plotly table,根据单元格值为其单元格着色,1 应替换为绿色,0 应替换为灰色。
此外,在绘制 table 时,列名和行索引应保持 table 无颜色。
结果table应该是
以上可视化可以使用 pandas 样式配置来实现,但是我如何在 Plotly-Dash 中实现相同的效果
此处提供的示例https://plotly.com/python/table/#cell-color-based-on-variable,
不展示如何在不着色的情况下保留行和列索引 names/values
- 它确实包含在 plotly table pandas 文档中
- 只需要扩展以在提供给 header* 和 cells
的数据中包含索引
- 加上符合您的样式要求的简单颜色矩阵
import pandas as pd
import numpy as np
import plotly.graph_objects as go
df = pd.DataFrame(
{
**{"index": [f"a{i+1}" for i in range(3)]},
**{
d: np.random.randint(0, 2, 3)
for d in pd.date_range("18-jul-2021", periods=4)
},
}
).set_index("index")
fig = go.Figure(
go.Table(
header={"values": [df.index.name] + df.columns.tolist()},
cells=dict(
values=df.reset_index().T.values,
fill_color=np.select(
[df.reset_index().T.values == 1, df.reset_index().T.values == 0],
["green", "red"],
"white",
),
align="center",
),
)
)
fig
我有一个数据框
如何将其绘制为 Plotly table,根据单元格值为其单元格着色,1 应替换为绿色,0 应替换为灰色。 此外,在绘制 table 时,列名和行索引应保持 table 无颜色。
结果table应该是
以上可视化可以使用 pandas 样式配置来实现,但是我如何在 Plotly-Dash 中实现相同的效果
此处提供的示例https://plotly.com/python/table/#cell-color-based-on-variable, 不展示如何在不着色的情况下保留行和列索引 names/values
- 它确实包含在 plotly table pandas 文档中
- 只需要扩展以在提供给 header* 和 cells 的数据中包含索引
- 加上符合您的样式要求的简单颜色矩阵
import pandas as pd
import numpy as np
import plotly.graph_objects as go
df = pd.DataFrame(
{
**{"index": [f"a{i+1}" for i in range(3)]},
**{
d: np.random.randint(0, 2, 3)
for d in pd.date_range("18-jul-2021", periods=4)
},
}
).set_index("index")
fig = go.Figure(
go.Table(
header={"values": [df.index.name] + df.columns.tolist()},
cells=dict(
values=df.reset_index().T.values,
fill_color=np.select(
[df.reset_index().T.values == 1, df.reset_index().T.values == 0],
["green", "red"],
"white",
),
align="center",
),
)
)
fig