great_tables
  • Get Started
  • Examples
  • Reference
  • Blog

Great Tables v0.2.0: Easy Data Coloring

Author

Rich Iannone

Published

January 24, 2024

We enjoy working on Great Tables because we want everybody to easily make beautiful tables. Tables don’t have to be boring, they really could be captivating and insightful. With every release we get closer and closer to realizing our mission and, as such, we’re happy to announce the v0.2.0 release that’s now on PyPI.

The really big feature that’s available with this release is the data_color() method. It gives you several options for colorizing data cells based on the underlying data. The method automatically scales color values according to the data in order to emphasize differences or reveal trends. The example below emphasizes large currency values with a "darkgreen" fill color.

from great_tables import GT, exibble

(
    GT(exibble[["currency", "date", "row"]].head(4), rowname_col="row")
    .data_color(
        columns="currency",
        palette=["lightblue", "darkgreen"]
    )
)
currency date
row_1 49.95 2015-01-15
row_2 17.95 2015-02-15
row_3 1.39 2015-03-15
row_4 65100.0 2015-04-15

Note that we use columns= to specify which columns get the colorizing treatment (just currency here) and the palette= is given as a list of color values. From this we can see that the 65100.0 value polarizes the data coloring process; it is "darkgreen" while all other values are "lightblue" (with no interpolated colors in between). Also, isn’t it nice that the text adapts to the background color?

The above example is suitable for emphasizing large values, but, maybe you consider the extreme value to be something that’s out of bounds? For that, we can use the domain= and na_value= arguments to gray-out the extreme values. We’ll also nicely format the currency column in this next example.

(
    GT(exibble[["currency", "date", "row"]].head(4), rowname_col="row")
    .data_color(
        columns="currency",
        palette=["lightblue", "darkgreen"],
        domain=[0, 50],
        na_color="lightgray"
    )
    .fmt_currency(
        columns="currency",
        currency="GBP",
        use_subunits=False
    )
)
currency date
row_1 £50 2015-01-15
row_2 £18 2015-02-15
row_3 £1 2015-03-15
row_4 £65,100 2015-04-15

Now the very large value is in "lightgray", making all other values easier to compare. We did setting domain=[0, 50] and specifying na_color="lightgray". This caused the out-of-bounds value of 65100 to have a light gray background. Notice that the values are also formatted as currencies, and this is thanks to fmt_currency() which never interferes with styling.

Here’s a more inspirational example that uses a heavily-manipulated version of the countrypops dataset (thanks again, Polars!) along with a color treatment that’s mediated by data_color(). Here, the population values can be easily compared by the amount of "purple" within them.

from great_tables.data import countrypops
import polars as pl
import polars.selectors as cs

wide_pops = (
    pl.from_pandas(countrypops)
    .filter(
        pl.col("country_code_2").is_in(["FM", "GU", "KI", "MH", "MP", "NR", "PW"])
        & pl.col("year").is_in([2000, 2010, 2020])
    )
    .pivot(index="country_name", on="year", values="population")
    .sort("2020", descending=True)
)

(
    GT(wide_pops, rowname_col="country_name")
    .tab_header(
        title="Populations of Select Countries in Oceania",
        subtitle="Population values are from 2000, 2010, and 2020.",
    )
    .tab_spanner(label="Total Population", columns=cs.all())
    .fmt_integer(columns=["2000", "2010", "2020"])
    .data_color(palette=["white", "purple"], domain=[0, 1.7e5])
)
Populations of Select Countries in Oceania
Population values are from 2000, 2010, and 2020.
Total Population
2000 2010 2020
Guam 160,188 164,905 169,231
Kiribati 88,826 107,995 126,463
Micronesia (Federated States) 111,709 107,588 112,106
Northern Mariana Islands 80,338 54,087 49,587
Marshall Islands 54,224 53,416 43,413
Palau 19,726 18,540 17,972
Nauru 10,377 10,241 12,315

This was just a sampler of what you can do with the all-new data_color() method. Take a look at these pages for more information:

  • The Colorizing with Data page in the Get Started Guide, which provides more details on how to use data_color()
  • The guide on Basic Styling covers general styling (e.g., bold text, underlines, etc.) with tab_style()
  • The reference pages for data_color() and tab_style()

To conclude, we’re happy that this new functionality is now in the Great Tables package! We hope you find it useful for your table-generation work. And we’ll keep improving upon it so that you’ll have more possibilities to make beautiful, and colorful, tables for presentation.