How to do a row wise data_color domain? #598
-
SummaryIs it possible to do a row wise data_color domain? For example, I have a table that summarizes my budget. Each row is a budget category (e.g. groceries), and each column is the month (e.g. Feb 2025). For each row I would like to visually identify the outliers with a brighter colour. Right now I when I use data_color it compares all values in the table. This is sometimes helpful, but not in the case of my budget. For example, my rent is always going to be much higher than my entertainment budget, but I don't want rent bright red each month. I only one rent bright red if I happen to spend more on rent on month Repreximport polars as pl
from great_tables import GT, loc, style
data = pl.DataFrame(
{
"category_name": ["groceries", "dining out"],
"group_name": ["Daily", "Daily"],
"Mar 2024": [577.95, 460.96000000000004],
"Apr 2024": [1286.5700000000002, 492.48],
"May 2024": [1290.26, 845.2500000000001],
"Jun 2024": [950.0300000000001, 637.59],
"Jul 2024": [993.1999999999999, 806.2000000000002],
"Aug 2024": [975.6599999999999, 479.18999999999994],
"Sep 2024": [1543.16, 558.7199999999999],
"Oct 2024": [682.6499999999999, 787.63],
"Nov 2024": [1158.2900000000002, 890.1],
"Dec 2024": [1445.88, 868.2400000000001],
"Total": [10903.65, 6826.360000000001],
}
)
(
GT(data, rowname_col="category_name", groupname_col="group_name")
.tab_header("Spending", subtitle="Monthly Spending by Category, Group, and Month")
.tab_style(
[style.fill("#7583FF"), style.text(color="white", weight="bold")],
loc.row_groups(),
)
.tab_style(
[style.text(weight="bold")], [loc.stubhead(), loc.column_labels(), loc.header()]
)
.tab_stubhead("Category")
.fmt_currency(decimals=0)
.fmt_markdown(pl.selectors.string())
.data_color(
domain=None,
palette=["white", "yellow", "red"],
na_color="white",
columns=(pl.selectors.numeric() - pl.selectors.contains("Total")),
)
) RelatedThis is similar to #581, but I think my question is slightly different. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found a solution that works. You can iterate over each row and add colour to the row: import polars as pl
from great_tables import GT, loc, style
data = pl.DataFrame(
{
"category_name": ["groceries", "dining out"],
"group_name": ["Daily", "Daily"],
"Mar 2024": [577.95, 460.96000000000004],
"Apr 2024": [1286.5700000000002, 492.48],
"May 2024": [1290.26, 845.2500000000001],
"Jun 2024": [950.0300000000001, 637.59],
"Jul 2024": [993.1999999999999, 806.2000000000002],
"Aug 2024": [975.6599999999999, 479.18999999999994],
"Sep 2024": [1543.16, 558.7199999999999],
"Oct 2024": [682.6499999999999, 787.63],
"Nov 2024": [1158.2900000000002, 890.1],
"Dec 2024": [1445.88, 868.2400000000001],
"Total": [10903.65, 6826.360000000001],
}
)
table = (
GT(data, rowname_col="category_name", groupname_col="group_name")
.tab_header("Spending", subtitle="Monthly Spending by Category, Group, and Month")
.tab_style(
[style.fill("#7583FF"), style.text(color="white", weight="bold")],
loc.row_groups(),
)
.tab_style(
[style.text(weight="bold")], [loc.stubhead(), loc.column_labels(), loc.header()]
)
.tab_stubhead("Category")
.fmt_currency(decimals=0)
.fmt_markdown(pl.selectors.string())
)
for i in range(0, data.shape[0]):
max_row_value = data[0].select(pl.selectors.numeric() - pl.selectors.contains("Total")).max_horizontal().to_list()[0]
table = (
table
.data_color(
domain=[0, max_row_value],
palette=["white", "yellow", "red"],
na_color="white",
rows=i,
columns=(pl.selectors.numeric() - pl.selectors.contains("Total")),
)
)
table |
Beta Was this translation helpful? Give feedback.
I found a solution that works. You can iterate over each row and add colour to the row: