posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.44k stars 50 forks source link

nanoplots: `data_area_fill_color` is identical across multiple columns #268

Open thomascamminady opened 3 months ago

thomascamminady commented 3 months ago

In the example below, I have specified the second data_area_fill_color to be blue, but it instead takes the value of the first column.

image

Since I'm allowed to pick two different data_line_stroke_color values, I thought the area should also work, but it seems like that is not the case. Here's a minimal working example:

import polars as pl
from great_tables import GT
from great_tables import nanoplot_options

(
    GT(
        pl.DataFrame(
            {
                "x": [1, 2, 3],
                "y1": ["1 2 3", "2 3 4", "3 4 5"],
                "y2": ["3 4 5", "4 5 6", "5 6 7"],
            }
        )
    )
    .fmt_nanoplot(
        columns="y1",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="red",
            data_area_fill_color="red",
        ),
    )
    .fmt_nanoplot(
        columns="y2",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="blue",
            data_area_fill_color="blue",
        ),
    )
)

If I switch the .fmt_nanoplot calls, to the snippet below, I still get the same table:

import polars as pl
from great_tables import GT
from great_tables import nanoplot_options

(
    GT(
        pl.DataFrame(
            {
                "x": [1, 2, 3],
                "y1": ["1 2 3", "2 3 4", "3 4 5"],
                "y2": ["3 4 5", "4 5 6", "5 6 7"],
            }
        )
    )
    .fmt_nanoplot(
        columns="y2",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="blue",
            data_area_fill_color="blue",
        ),
    )
    .fmt_nanoplot(
        columns="y1",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="red",
            data_area_fill_color="red",
        ),
    )
)

However, making the y1 column be blue results in the full table being blue.

(
    GT(
        pl.DataFrame(
            {
                "x": [1, 2, 3],
                "y1": ["1 2 3", "2 3 4", "3 4 5"],
                "y2": ["3 4 5", "4 5 6", "5 6 7"],
            }
        )
    )
    .fmt_nanoplot(
        columns="y1",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="blue",
            data_area_fill_color="blue",
        ),
    )
    .fmt_nanoplot(
        columns="y2",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="red",
            data_area_fill_color="red",
        ),
    )
)
image

So presumably, this is about the order in which the columns appear in the dataframe since


(
    GT(
        pl.DataFrame(
            {
                "x": [1, 2, 3],
                "y2": ["3 4 5", "4 5 6", "5 6 7"],
                "y1": ["1 2 3", "2 3 4", "3 4 5"],
            }
        )
    )
    .fmt_nanoplot(
        columns="y1",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="blue",
            data_area_fill_color="blue",
        ),
    )
    .fmt_nanoplot(
        columns="y2",
        autoscale=False,
        options=nanoplot_options(
            data_point_radius=0,
            data_line_stroke_color="red",
            data_area_fill_color="red",
        ),
    )
)

again gives a red area.

I'm running great-tables = "^0.4.0".

rich-iannone commented 3 months ago

Thanks for providing this detailed bug report! I think I know what the underlying problem is, so I’ll prepare a fix for this soon.

thomascamminady commented 3 months ago

Awesome! Thanks a lot. I gave it a try and looked through the code but I'm clearly not smart enough. Great work with great_tables!