Skip to content

API: plotting

apply_style(color_scheme='fastf1', timedelta_support=True)

Apply a consistent Matplotlib style.

Uses FastF1's built-in styling (dark theme by default) and a few readability tweaks.

Source code in src/fastf1_analytics/plotting.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def apply_style(
    color_scheme: str | None = "fastf1",
    timedelta_support: bool = True,
) -> None:
    """Apply a consistent Matplotlib style.

    Uses FastF1's built-in styling (dark theme by default) and a few readability tweaks.
    """
    f1plot.setup_mpl(mpl_timedelta_support=timedelta_support, color_scheme=color_scheme)
    plt.rcParams.update(
        {
            # readability
            "figure.dpi": 160,
            "axes.titlesize": 14,
            "axes.labelsize": 12,
            "legend.frameon": False,
            "axes.grid": True,
            "grid.linestyle": "--",
            "grid.alpha": 0.25,
            # savefig defaults
            "savefig.bbox": "tight",
        }
    )

fmt_laptime_seconds(sec)

Format seconds as M:SS.sss (e.g., 73.456 -> '1:13.456').

Source code in src/fastf1_analytics/plotting.py
103
104
105
106
def fmt_laptime_seconds(sec: float) -> str:
    """Format seconds as M:SS.sss (e.g., 73.456 -> '1:13.456')."""
    m, s = divmod(float(sec), 60.0)
    return f"{int(m)}:{s:06.3f}"

get_compound_color(compound)

Return hex color for a tyre compound (FastF1 or fallback).

Source code in src/fastf1_analytics/plotting.py
125
126
127
128
129
130
131
132
133
134
135
136
137
def get_compound_color(compound: str) -> str:
    """Return hex color for a tyre compound (FastF1 or fallback)."""
    c = compound.strip().upper()
    # Prefer FastF1 if available
    try:
        # FastF1 exposes a mapping in plotting; normalize keys when present
        mapping = getattr(f1plot, "COMPOUND_COLORS", None)
        if isinstance(mapping, dict):
            # keys in FastF1 are typically 'SOFT','MEDIUM','HARD','INTERMEDIATE','WET'
            return cast(str, mapping.get(c, _COMPOUND_FALLBACK.get(c, "#888888")))
    except Exception:
        pass
    return _COMPOUND_FALLBACK.get(c, "#888888")

get_driver_color(driver, *, session=None)

Return the (team) color for a driver abbreviation in a given session.

Source code in src/fastf1_analytics/plotting.py
 98
 99
100
def get_driver_color(driver: str, *, session: Any | None = None) -> str:
    """Return the (team) color for a driver abbreviation in a given session."""
    return cast(str, f1plot.get_driver_color(driver, session=session))

get_team_color(team, *, session=None)

Return a hex color for a team. Tries FastF1's mapping; falls back to a local mapping so charts work even without a session object or for alternate team names.

Source code in src/fastf1_analytics/plotting.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def get_team_color(team: str, *, session: Any | None = None) -> str:
    """Return a hex color for a team.
    Tries FastF1's mapping; falls back to a local mapping so charts work even
    without a session object or for alternate team names.
    """
    if not team:
        return "#888888"
    try:
        # Prefer FastF1 when available; it knows historic liveries/aliases.
        return cast(str, f1plot.get_team_color(team, session=session))
    except Exception:
        # Public mapping if present in this FastF1 version
        mapping = getattr(f1plot, "TEAM_COLORS", None)
        if isinstance(mapping, dict) and team in mapping:
            return cast(str, mapping[team])
        # Normalize and try our local fallback
        return _TEAM_COLOR_FALLBACK.get(_norm_key(team), "#888888")

lighten_color(color, amount=0.25)

Return a lightened variant of a hex color (used as a 'secondary' line color).

amount=0 keeps the same color; amount=1 goes to white.

Source code in src/fastf1_analytics/plotting.py
140
141
142
143
144
145
146
147
148
149
def lighten_color(color: str, amount: float = 0.25) -> str:
    """Return a lightened variant of a hex color (used as a 'secondary' line color).

    amount=0 keeps the same color; amount=1 goes to white.
    """
    r, g, b = to_rgb(color)
    r = r + (1.0 - r) * amount
    g = g + (1.0 - g) * amount
    b = b + (1.0 - b) * amount
    return to_hex((r, g, b))

savefig(fig, path, *, dpi=220)

Save figure to path with consistent DPI and tight layout; ensure parent dir exists.

Source code in src/fastf1_analytics/plotting.py
152
153
154
155
156
157
158
159
160
161
def savefig(fig: Figure, path: str | Path, *, dpi: int = 220) -> Path:
    """Save figure to *path* with consistent DPI and tight layout; ensure parent dir exists."""
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    try:
        fig.tight_layout()
    except Exception:
        pass
    fig.savefig(p, dpi=dpi)
    return p

seconds_formatter()

Matplotlib formatter that renders seconds as M:SS.sss.

Source code in src/fastf1_analytics/plotting.py
109
110
111
def seconds_formatter() -> FuncFormatter:
    """Matplotlib formatter that renders seconds as M:SS.sss."""
    return FuncFormatter(lambda x, pos: fmt_laptime_seconds(x))

Examples

from fastf1_analytics.plotting import get_team_color, get_compound_color, savefig, apply_style
apply_style()
print(get_team_color("Ferrari"))
print(get_compound_color("Soft"))