Using ColumnDefinition#

You can style a column by providing a ColumnDefinition and providing it to the table argument

    from plottable import ColumnDefinition

    Table(df, column_definitions: List[ColumnDefinition]=None)

when instantiating the Table.

You can also use the alias ColDef

    from plottable import ColDef
class ColumnDefinition:
    """A Class defining attributes for a table column.

    Attributes:
        name: str:
            the column name
        title: str = None:
            the plotted title to override the column name
        width: float = 1:
            the width of the column as a factor of the default width
        textprops: Dict[str, Any] = field(default_factory=dict)
            textprops provided to each textcell
        formatter: Callable = None:
            A Callable to format the appearance of the texts
        cmap: Callable = None:
            A Callable that returns a color based on the cells value.
        text_cmap: Callable = None:
            A Callable that returns a color based on the cells value.
        group: str = None:
            Each group will get a spanner column label above the column labels.
        plot_fn: Callable = None
            A Callable that will take the cells value as input and create a subplot
            on top of each cell and plot onto them.
            To pass additional arguments to it, use plot_kw (see below).
        plot_kw: Dict[str, Any] = field(default_factory=dict)
            Additional keywords provided to plot_fn.
        border: str | List = None:
            Plots a vertical borderline.
            can be either "left" / "l", "right" / "r" or "both"
    """


Setting Column Titles#

You can choose a different title for your Column with the title argument.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from plottable import ColumnDefinition, ColDef, Table

d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", title="Title A")])

plt.show()
../_images/34ab6ed6592b3e5c84cb6f23824a2d2e6d9a3f155d0eb81b8d903e595ee64e76.png

Setting Column Widths#

You can provide a width (float) argument to a ColumnDefinition. It’s the width as a factor of the default column width.
By default column widths are calculated by dividing the width of the axes into n_columns same widths.

d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", width=2)])

plt.show()
../_images/6d92d35a6a35d60f7dfbf1abacc69c866bdbdaa2aa308b7238302d9836b58dd2.png

Setting Column Textprops#

You can provide a textprops (Dict[str: Any]) argument to a ColumnDefinition to adjust the font attributes.

d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="index", textprops={"ha": "left", "weight": "bold"})])

plt.show()
../_images/091e0c7a9c1a687bd32d08cb4eee13015eb692a98c5cd98ea61d77ec783816ba.png

Providing a Text Formatter#

By providing a formatter (Callable) to a ColumnDefinition you can adjust the appearance of the plotted cell texts.

from plottable.formatters import decimal_to_percent

d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", formatter=decimal_to_percent)])

plt.show()
../_images/548932f2c1e0f035ed21abcfb54c1e0e0a8d2bcfe4df8be1747fd10771cadc7c.png

Providing a cmap function#

By providing a cmap (Callable) to a ColumnDefinition you can adjust the appearance of the cell backgrounds based on the cells value.

from plottable.cmap import normed_cmap
import matplotlib.cm


d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", cmap=matplotlib.cm.Reds)])

plt.show()
../_images/68f519667b40ea277b963f298d7111cb5edd4aef905b93b91e7a81350b401ae5.png

Providing a text_cmap function#

By providing a text_cmap (Callable) to a ColumnDefinition you can adjust the color of the plotted cell texts based on the cells value.

from plottable.cmap import normed_cmap
import matplotlib.cm


d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", text_cmap=matplotlib.cm.Reds)])

plt.show()
../_images/b36bcfa008228044b9d223f04292bed957fb0747ca47938ad4e9ca1d108c1994.png

Providing a group argument#

By providing a group (str) to a ColumnDefinition the group will get a spanner column label above the column labels for spanning all Columns with a shared group.

from plottable.cmap import normed_cmap
import matplotlib.cm


d = pd.DataFrame(np.random.random((5, 5)), columns=["A", "B", "C", "D", "E"]).round(2)
fig, ax = plt.subplots(figsize=(6, 5))
tab = Table(d, column_definitions=[ColumnDefinition(name="A", group="group_name"), 
                                   ColumnDefinition(name="B", group="group_name")])

plt.show()
../_images/934dedb84b1e96463421e73fb0ba8358c9a1eecf887be9ec8463be97add7220f.png

Providing a plot function#

By providing a plot_fn (Callable) to a ColumnDefinition, plottable creates an axes on top of each Cell of this column, and creates plots based on each cell’s value.

You can provide additional keywords to the plot function by passing a plot_kw dictionary to ColumnDefinition.


    plot_fn: Callable = None
        A Callable that will take the cells value as input and create a subplot
        on top of each cell and plot onto them.
        To pass additional arguments to it, use plot_kw (see below).
    plot_kw: Dict[str, Any] = field(default_factory=dict)
        Additional keywords provided to plot_fn.

For available plotting functions have a look at the Plots Notebook

Providing a border argument#

By providing a border (str) argument, you can adjust whether left and right borders of all Columns cells are plotted.

border can be either “left” / “l”, “right” / “r” or “both”

See also

The Women’s World Cup Example uses most of the available ColumnDefinition arguments.