znstrider / plottable

most pretty & lovely tables with matplotlib
MIT License
244 stars 13 forks source link

progress_donut and decimal_to_percent don't work together #1

Open rjtavares opened 1 year ago

rjtavares commented 1 year ago

I changed the ColumnDefinition for one column on the wwc example, from:

+ [
    ColumnDefinition(
        name=knockout_stage_cols[0],
        title=knockout_stage_cols[0].replace(" ", "\n", 1),
        formatter=decimal_to_percent,
        cmap=cmap,
        group="Knockout Stage Chances",
        border="left",
    )
]

to:

+ [
    ColumnDefinition(
        name=knockout_stage_cols[0],
        title=knockout_stage_cols[0].replace(" ", "\n", 1),
        formatter=decimal_to_percent,
        group="Knockout Stage Chances",
        border="left",
        plot_fn=progress_donut,
        plot_kw={'is_pct': True}
    )
]

It outputs an error:

File ~\mambaforge\envs\fastai\lib\site-packages\mpltable\table.py:469, in Table._apply_column_formatters(self)
    467 if formatter is not None:
    468     for cell in self.columns[colname].cells:
--> 469         cell.text.set_text(formatter(cell.content))

AttributeError: 'SubplotCell' object has no attribute 'text'

Commenting out the formatter solves the issue (but keeps the ugly format). Intuitively though, this combination should work together.

znstrider commented 1 year ago

Thanks again for finding this!

I opted to add formatter arguments to plot functions that add a text object (bar and progress_donut). Now a formatter can easily be provided to the plot_kw as such:

+ [
    ColumnDefinition(
        name=knockout_stage_cols[0],
        title=knockout_stage_cols[0].replace(" ", "\n", 1),
        # formatter=decimal_to_percent, # this won't raise anymore but won't do anything
                                        # because a SubplotCell with no text property is created
        group="Knockout Stage Chances",
        border="left",
        plot_fn=progress_donut,
        plot_kw={'is_pct': True,  'formatter': decimal_to_percent} # <-----
    )
]

This keeps providing a formatter to a text column seperate from providing one to the plot function.

It would be an option to check whether a plot function and a formatter are provided, and to pass the formatter onto the plot_kw dictionary automatically if this is more intuitive.

I'll leave this open for now.