Visualize#

This module contains functions for visualizing the tracking and event data.

Plot Soccer Pitch#

databallpy.visualize.plot_soccer_pitch

plot_soccer_pitch(field_dimen: tuple[float, float] = (106.0, 68.0), pitch_color: str = 'mediumseagreen', linewidth: int = 2, markersize: int = 20, fig: figure = None, ax: axes = None) tuple[figure, axes][source]#

A function to plot a soccer pitch Note: relies heavily on Friends-of-Tracking-Data-FoTD/ LaurieOnTracking/blob/master/Metrica_Viz.py

Parameters:
  • field_dimen (tuple, optional) – x and y length of pitch in meters. Defaults to (106.0, 68.0).

  • pitch_color (str, optional) – Color of the pitch. Defaults to “mediumseagreen”.

  • linewidth (int, optional) – Width of the lines on the pitch. Defaults to 2.

  • markersize (int, optional) – Size of the dots on the pitch. Defaults to 20.

  • fig (plt.figure, optional) – Figure to plot the pitch on. Defaults to None.

  • ax (plt.axes, optional) – Axes to plot the pitch on. Defaults to None.

Returns:

figure and axes with the pitch depicted on it

Return type:

Tuple[plt.figure, plt.axes]

Plot Events#

databallpy.visualize.plot_events

plot_events(game: Game, events: list[str] = [], outcome: bool | None = None, player_ids: list[str] = [], team_id: int | str | None = None, fig: figure = None, ax: axes = None, color_by_col: str = None, team_colors: list[str] = ['orange', 'red'], title: str | None = None) tuple[figure, axes][source]#

Function to plot the locations of specific events

Parameters:
  • game (Game) – All information about a game

  • events (list, optional) – Filter of events you want to plot, if empty, all events are plotted. Defaults to [].

  • outcome (bool, optional) – Filter if the event should have a succesfull outcome (True) or not (False), if None, all outcomes are included. Defaults to None.

  • player_ids (list, optional) – Filter for what players to include, if empty, all players are included. Defaults to [].

  • team_id (int | str, optional) – Filter for what team to include, if None, both teams are included. Defaults to None.

  • fig (plt.figure, optional) – Figure to plot the events on. Defaults to None.

  • ax (plt.axes, optional) – Axes to plot the events on. Defaults to None.

  • color_by_col (str, optional) – If specified, colors of scatter is specified by this colom in game.event_data. Defaults to None.

  • team_colors (list, optional) – Colors by which the teams should be represented. Defaults to [“orange”, “red”].

  • title (str, optional) – Title of the plot. Defaults to None.

Returns:

figure and axes with the pitch and events depicted on it.

Return type:

Tuple[plt.figure, plt.axes]

Plot Tracking Data#

databallpy.visualize.plot_tracking_data

plot_tracking_data(game: Game, idx: int, team_colors: list[str] = ['green', 'red'], *, fig: figure = None, ax: axes = None, title: str = None, events: list = [], variable_of_interest: any = None, add_player_possession: bool = False, add_velocities: bool = False, heatmap_overlay: ndarray | None = None, overlay_cmap: Colormap | str = 'viridis') tuple[figure, axes][source]#

Function to plot the tracking data of a specific index in the game.tracking_data.

Parameters:
  • game (Game) – Game with tracking data and other info of the game.

  • idx (int) – Index of the tracking data you want to plot.

  • team_colors (list[str], optional) – The color of the teams. Defaults to [“green”, “red”].

  • fig (plt.figure, optional) – The figure to plot on. Defaults to None.

  • ax (plt.axes, optional) – The axes to plot on. Defaults to None.

  • title (str, optional) – The title of the plot. Defaults to None.

  • events (list, optional) – The databallpy events to plot. Defaults to [].

  • variable_of_interest (any, optional) – The variable you want to plot. Defaults to None.

  • add_player_possession (bool, optional) – Whether to add a circle around the player that has possession over the ball. Defaults to False.

  • add_velocities (bool, optional) – Whether to add the velocities to the plot. Defaults to False.

  • heatmap_overlay (np.ndarray, optional) – A heatmap to overlay on the pitch. Defaults to None.

  • overlay_cmap (Colormap | str, optional) – The colormap to use for the heatmap overlay. Defaults to “viridis”.

Returns:

The figure and axes with the tracking data

Return type:

tuple[plt.figure, plt.axes]

Video#

databallpy.visualize.save_tracking_video

save_tracking_video(game: Game, start_idx: int, end_idx: int, save_folder: str, *, title: str = 'test_clip', team_colors: list = ['green', 'red'], events: list = [], variable_of_interest: Series | list = None, add_player_possession: bool = False, add_velocities: bool = False, heatmap_overlay: ndarray | None = None, overlay_cmap: Colormap | str = 'viridis', verbose: bool = True)[source]#

Function to save a subset of a game clip of the tracking data.

Note that making animation is build with FFMPEG. You need to have FFMPEG installed on you device before being able to use this function.

Parameters:
  • game (Game) – Game with tracking data and ohter info of the game.

  • start_idx (int) – Start index of what to save of the game.tracking_data df.

  • end_idx (int) – End index of what to save of the game.tracking_data df.

  • save_folder (str) – Location where to save the clip.

  • title (str, optional) – Title of the clip. Defaults to “test_clip”.

  • team_colors (list, optional) – Colors of the home and away team. Defaults to [“green”, “red”].

  • events (list, optional) – What events should be plotted as well. Defaults to [].

  • variable_of_interest (pd.Series | list, optional) – Variable you want to have plotted in the clip, this is a pd.Series that should have the same index (start_idx:end_idx) as the tracking data that will be plotted. Defaults to None.

  • add_player_possession (bool, optional) – Whether to add a mark around which player has possession over the ball. Defaults to False. If True, the column ‘player_possession’ should be in the game.tracking_data.

  • add_velocities (bool, optional) – Whether or not to add the velocities to the clip, will add arrows to show the velocity. Defaults to False.

  • heatmap_overlay (np.ndarray, optional) – A heatmap to overlay on the pitch. Defaults to None.

  • overlay_cmap (Colormap | str, optional) – The colormap to use for the heatmap overlay. Defaults to “viridis”.

  • verbose (bool, optional) – Whether or not to print info in the terminal on the progress