Skip to content

look

champalimaud.look

The look every figure shares: colors and the plotting defaults.

No data and no network. use_base_look sets the defaults, so a plot states a color, size, or line width only when its data need it.

type_color(field='type')

Build an altair color encoding from TYPE_COLORS.

altair assigns colors to categories in alphabetical order. This encoding fixes the domain and the colors, with no legend.

Parameters:

Name Type Description Default
field str

Nominal column that holds the type of each mark.

"type"

Returns:

Type Description
Color

The encoding, to pass as color= in encode.

Examples:

>>> type_color("kind").shorthand
'kind:N'
Source code in champalimaud/look.py
def type_color(field: str = "type") -> alt.Color:
    """Build an altair color encoding from `TYPE_COLORS`.

    altair assigns colors to categories in alphabetical order.
    This encoding fixes the domain and the colors, with no legend.

    Parameters
    ----------
    field : str, default "type"
        Nominal column that holds the type of each mark.

    Returns
    -------
    altair.Color
        The encoding, to pass as ``color=`` in ``encode``.

    Examples
    --------
    >>> type_color("kind").shorthand
    'kind:N'
    """
    return alt.Color(
        f"{field}:N",
        scale=alt.Scale(
            domain=list(TYPE_COLORS), range=list(TYPE_COLORS.values())
        ),
        legend=None,
    )

use_base_look()

Set the matplotlib defaults and enable the altair theme "lab".

The matplotlib color cycle follows TYPE_COLORS, with no top and right spines, no legend frame, and constrained layout. The altair theme lab has the same colors.

Examples:

>>> use_base_look()
>>> plt.rcParams["axes.spines.top"]
False
Source code in champalimaud/look.py
def use_base_look() -> None:
    """Set the matplotlib defaults and enable the altair theme "lab".

    The matplotlib color cycle follows `TYPE_COLORS`, with no top and
    right spines, no legend frame, and constrained layout.
    The altair theme ``lab`` has the same colors.

    Examples
    --------
    >>> use_base_look()
    >>> plt.rcParams["axes.spines.top"]
    False
    """
    plt.rcParams.update(
        {
            "axes.prop_cycle": cycler(color=list(TYPE_COLORS.values())),
            "axes.spines.top": False,
            "axes.spines.right": False,
            "legend.frameon": False,
            "figure.constrained_layout.use": True,
        }
    )

    @alt.theme.register("lab", enable=True)
    def lab_theme() -> alt.theme.ThemeConfig:
        return alt.theme.ThemeConfig(
            {
                "config": {
                    "view": {"stroke": "transparent"},
                    "range": {"category": list(TYPE_COLORS.values())},
                }
            }
        )