Skip to main content
The tif1.plotting module provides utilities for creating F1-themed visualizations with team and driver colors, compound colors, and matplotlib styling.

Setup

setup_mpl

Configure matplotlib with F1-themed styling.
def setup_mpl(
    color_scheme="fastf1",
    misc_mpl_mods=True,
    mpl_timedelta_support=False,
    **kwargs
) -> None
```python

**Parameters:**
- `color_scheme`: Color scheme to use (currently only "fastf1" is supported)
- `misc_mpl_mods`: Apply miscellaneous matplotlib modifications
- `mpl_timedelta_support`: Enable timedelta support (compatibility parameter)

**Example:**
```python
import tif1
import matplotlib.pyplot as plt

# Apply F1 dark theme
tif1.plotting.setup_mpl(color_scheme="fastf1")

# Now all plots will use the dark F1 theme
session = tif1.get_session(2025, "Monaco Grand Prix", "Qualifying")
laps = session.laps
plt.plot(laps["LapNumber"], laps["LapTime"])
plt.show()
```python

---

## Color Functions

### `get_team_color`

Get the official color for a team.

```python
def get_team_color(team: str, session=None) -> str
```python

**Parameters:**
- `team`: Team name (e.g., "Red Bull Racing", "Ferrari")
- `session`: Optional session object (for future compatibility)

**Returns:**
- Hex color code (e.g., "#dc0000" for Ferrari)

**Example:**
```python
from tif1.plotting import get_team_color

ferrari_color = get_team_color("Ferrari")
# Returns: "#dc0000"
```python

### `get_driver_color`

Get the color for a specific driver.

```python
def get_driver_color(driver: str, session=None) -> str
```python

**Parameters:**
- `driver`: 3-letter driver code (e.g., "VER", "HAM")
- `session`: Optional session object

**Returns:**
- Hex color code

**Example:**
```python
from tif1.plotting import get_driver_color

ver_color = get_driver_color("VER")
# Returns: "#0600ef" (Red Bull blue)
```python

### `get_driver_color_mapping`

Get a complete mapping of driver codes to colors.

```python
def get_driver_color_mapping(session=None) -> dict[str, str]
```python

**Parameters:**
- `session`: Optional session object to extract team colors for all drivers

**Returns:**
- Dictionary mapping driver codes to hex colors

**Example:**
```python
from tif1.plotting import get_driver_color_mapping

session = tif1.get_session(2025, "Bahrain Grand Prix", "Race")
colors = get_driver_color_mapping(session)

# Plot each driver with their team color
for driver in session.drivers:
    driver_laps = session.get_driver(driver).laps
    plt.plot(
        driver_laps["LapNumber"],
        driver_laps["LapTime"],
        color=colors.get(driver, "#ffffff"),
        label=driver
    )
```python ### `get_compound_color`

Get the color for a tire compound.

```python
def get_compound_color(compound: str, session=None) -> str
```python

**Parameters:**
- `compound`: Compound name (e.g., "SOFT", "MEDIUM", "HARD")
- `session`: Optional session object

**Returns:**
- Hex color code

**Example:**
```python
from tif1.plotting import get_compound_color

soft_color = get_compound_color("SOFT")
# Returns: "#da291c" (red)
```python

### `get_compound_mapping`

Get a complete mapping of compounds to colors.

```python
def get_compound_mapping(session=None) -> dict[str, str]
```python

**Returns:**
- Dictionary mapping compound names to hex colors

---

## Styling Functions

### `get_driver_style`

Build a driver style dictionary for plotting.

```python
def get_driver_style(
    identifier: str,
    style=None,
    session=None
) -> dict[str, Any]
```python

**Parameters:**
- `identifier`: Driver code
- `style`: Optional style specification (list of keys or dict)
- `session`: Optional session object

**Returns:**
- Dictionary with color and linestyle

**Example:**
```python
from tif1.plotting import get_driver_style

style = get_driver_style("VER")
# Returns: {"color": "#0600ef", "linestyle": "-"}

plt.plot(data, **style)
```python

### `add_sorted_driver_legend`

Add a legend with driver labels sorted alphabetically.

```python
def add_sorted_driver_legend(ax, session=None)
```python

**Parameters:**
- `ax`: Matplotlib axes object
- `session`: Optional session object

**Returns:**
- Legend object

**Example:**
```python
import matplotlib.pyplot as plt
from tif1.plotting import add_sorted_driver_legend

fig, ax = plt.subplots()
# ... plot driver data ...
add_sorted_driver_legend(ax)
plt.show()
```yaml

---

## Color Constants

### Team Colors

```python
TEAM_COLORS = {
    "Mercedes": "#00d2be",
    "Red Bull Racing": "#0600ef",
    "Ferrari": "#dc0000",
    "McLaren": "#ff8700",
    "Alpine": "#0090ff",
    "Aston Martin": "#006f62",
    "RB": "#6692ff",
    "Haas": "#ffffff",
    "Sauber": "#52e252",
    "Williams": "#005aff",
}
```python ### Compound Colors

```python
COMPOUND_COLORS = {
    "SOFT": "#da291c",
    "MEDIUM": "#ffd12e",
    "HARD": "#f0f0ec",
    "INTERMEDIATE": "#43b02a",
    "WET": "#0067ad",
    "UNKNOWN": "#8a8a8a",
}
```python

---

## Complete Example

```python
import tif1
import matplotlib.pyplot as plt
from tif1.plotting import setup_mpl, get_driver_color_mapping

# Setup F1 theme
setup_mpl(color_scheme="fastf1")

# Load session
session = tif1.get_session(2025, "Monaco Grand Prix", "Qualifying")
laps = session.laps

# Get driver colors
colors = get_driver_color_mapping(session)

# Plot fastest laps for top 3 drivers
fig, ax = plt.subplots(figsize=(12, 6))

for driver in ["VER", "LEC", "HAM"]:
    driver_laps = laps[laps["Driver"] == driver]
    ax.plot(
        driver_laps["LapNumber"],
        driver_laps["LapTime"],
        color=colors.get(driver, "#ffffff"),
        label=driver,
        linewidth=2
    )

ax.set_xlabel("Lap Number")
ax.set_ylabel("Lap Time (s)")
ax.set_title("Qualifying Lap Times - Top 3")
ax.legend()
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()
```python