> ## Documentation Index
> Fetch the complete documentation index at: https://tif1.tracinginsights.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gear Shifts on Track

> Visualizing which gear is used at each point on the circuit

Visualizing gear usage across a lap provides insight into corner characteristics, acceleration zones, and the technical demands of a circuit. This tutorial shows how to create a color-coded track map where each segment is colored by the gear being used.

<img src="https://mintcdn.com/tracinginsightscom/UgQgVDzHEEleARPO/assets/gear_shifts_on_track.png?fit=max&auto=format&n=UgQgVDzHEEleARPO&q=85&s=a9fc8549456e15c98a3c441aae5cce50" alt="Gear shifts visualization on track layout" width="1376" height="1183" data-path="assets/gear_shifts_on_track.png" />

## Loading the session

We'll analyze the fastest qualifying lap from the 2021 Austrian Grand Prix.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import tif1
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colormaps
from matplotlib.collections import LineCollection

# Enable plotting support
tif1.plotting.setup_mpl(mpl_timedelta_support=False, color_scheme='fastf1')

# Load qualifying session
session = tif1.get_session(2021, 'Austrian Grand Prix', 'Q')
```

## Getting telemetry data

Extract the fastest lap and its telemetry, which includes X/Y coordinates and gear data.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
lap = session.laps.pick_fastest()
tel = lap.get_telemetry()
```

## Preparing the data

Convert telemetry to numpy arrays and create line segments for plotting.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Extract X and Y coordinates
x = np.array(tel['X'].values)
y = np.array(tel['Y'].values)

# Create line segments from consecutive points
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

# Get gear data as float array
gear = tel['nGear'].to_numpy().astype(float)
```

## Creating the visualization

Use matplotlib's `LineCollection` to color each segment by gear.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Create a segmented colormap
cmap = colormaps['Paired']
lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N + 1), cmap=cmap)
lc_comp.set_array(gear)
lc_comp.set_linewidth(4)

# Create the plot
fig, ax = plt.subplots(figsize=(10, 8))
ax.add_collection(lc_comp)
ax.axis('equal')
ax.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)

plt.suptitle(
    f"Fastest Lap Gear Shift Visualization\n"
    f"{lap['Driver']} - {session.event['EventName']} {session.event.year}"
)
```

## Adding a colorbar

Add a colorbar to show which color corresponds to each gear.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# Add colorbar with centered ticks for each gear
cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
cbar.set_ticks(np.arange(1.5, 9.5))
cbar.set_ticklabels(np.arange(1, 9))

plt.show()
```

## Analyzing gear usage

When examining gear shift patterns, look for:

* **Low-speed corners:** Gears 1-3 indicate tight corners requiring heavy braking
* **Medium-speed corners:** Gears 4-5 show flowing sections where momentum is maintained
* **High-speed sections:** Gears 6-8 reveal straights and fast corners
* **Shift points:** Transitions between colors show where drivers change gears

Different circuits have distinct gear usage profiles. Street circuits like Monaco use lower gears extensively, while high-speed tracks like Monza spend more time in top gears.

## Complete example

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import tif1
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colormaps
from matplotlib.collections import LineCollection

# Setup
tif1.plotting.setup_mpl(mpl_timedelta_support=False, color_scheme='fastf1')

# Load session
session = tif1.get_session(2021, 'Austrian Grand Prix', 'Q')

# Get fastest lap and telemetry
lap = session.laps.pick_fastest()
tel = lap.get_telemetry()

# Prepare data
x = np.array(tel['X'].values)
y = np.array(tel['Y'].values)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
gear = tel['nGear'].to_numpy().astype(float)

# Create line collection
cmap = colormaps['Paired']
lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N + 1), cmap=cmap)
lc_comp.set_array(gear)
lc_comp.set_linewidth(4)

# Plot
fig, ax = plt.subplots(figsize=(10, 8))
ax.add_collection(lc_comp)
ax.axis('equal')
ax.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)

plt.suptitle(
    f"Fastest Lap Gear Shift Visualization\n"
    f"{lap['Driver']} - {session.event['EventName']} {session.event.year}"
)

# Add colorbar
cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
cbar.set_ticks(np.arange(1.5, 9.5))
cbar.set_ticklabels(np.arange(1, 9))

plt.show()
```

## Next steps

* Combine gear data with throttle/brake to analyze driving technique
* Compare gear usage between different drivers on the same circuit
* Analyze how gear usage changes between qualifying and race conditions
* Explore other track visualizations like speed or throttle maps
