Python library written on top of matplotlib library for customizable nfl charts
Project description
NFL Field Visualization and Animation Library
This library provides utilities to visualize a football field, plot player positions, and animate plays. It is designed to handle data from tracking datasets, such as those used in football analytics.
Installing the library:
pip install nfl_tracks
After successfully installing the nfl_tracks package, import it in the file using the code below:
# Import nfl_tracks
from nfl import visuals
Table of Contents
- Function Overview
- field()
- snap()
- play_game()
- Usage Examples
- Error Handling
- Customization Options
1. Function Overview
field()
Generates a plot of a football field with customizable features.
field(yard_numbers=True, touchdown_markings=True, fifty_yard=False, fig_size=(12, 6.33))
Parameters:
- yard_numbers (bool, default True): Whether to display yard numbers on the field.
- touchdown_markings (bool, default True): Whether to show touchdown zone labels.
- fifty_yard (bool, default False): Highlights the 50-yard line in gold.
- fig_size (tuple, default (12, 6.33)): Specifies the figure size.
Returns:
- fig (matplotlib.figure.Figure): The figure object.
- ax (matplotlib.axes.Axes): The axes object containing the plot.
Example Usage:
fig, ax = visuals.field()
plt.show()
snap()
Plots the positions of players during a specific frame of a play.
snap(data, gameId, playId, frameId, yard_numbers=True, touchdown_markings=True, fifty_yard=False, fig_size=(12, 6.33), **kwargs)
Parameters:
- data (pd.DataFrame): Contains tracking data with columns including gameId, playId, frameId, x, y, and club.
- gameId (int): Identifier for the game.
- playId (int): Identifier for the play.
- frameId (int): Identifier for the specific frame within the play.
- yard_numbers (bool, default True): Whether to display yard numbers on the field.
- touchdown_markings (bool, default True): Whether to show touchdown zone labels.
- fifty_yard (bool, default False): Highlights the 50-yard line in gold.
- fig_size (tuple, default (12, 6.33)): Specifies the figure size.
- **kwargs: Additional arguments
- size (int, default 10): Marker size for players.
- club_colors (dict): Custom mapping of club indices to colors.
Returns:
- fig (matplotlib.figure.Figure): The figure object.
- ax (matplotlib.axes.Axes): The axis object.
Example Usage:
fig, ax = visuals.snap(data, gameId=2022091200, playId=64, frameId=10)
plt.show()
play_game()
Animates a play by visualizing player movements over time.
play_game(data, gameId, playId, kaggle=True, yard_numbers=True, touchdown_markings=True, fifty_yard=False, fig_size=(12, 6.33), save=False, loop=False, **kwargs)
Parameters:
- data (pd.DataFrame): Contains tracking data with columns including gameId, playId, frameId, x, y, and club.
- gameId (int): Identifier for the game.
- playId (int): Identifier for the play.
- kaggle (bool, default True): Enables compatibility with Kaggle's notebook environments.
- yard_numbers (bool, default True): Whether to display yard numbers on the field.
- touchdown_markings (bool, default True): Whether to show touchdown zone labels.
- fifty_yard (bool, default False): Highlights the 50-yard line in gold.
- fig_size (tuple, default (12, 6.33)): Specifies the figure size.
- save (bool, default False): Saves the animation as a play.gif file if True.
- loop (bool, default False): Repeats the animation if True.
- **kwargs: Additional arguments
- size (int, default 10): Marker size for players.
- speed (int, default 100): Time interval between frames (in milliseconds).
- club_colors (dict): Custom mapping of club indices to colors.
- save_params (dict): Parameters for saving the animation.
Returns:
- ani (matplotlib.animation.FuncAnimation): The animation object.
Example Usage:
ani = visuals.play_game(data, gameId=2022091200, playId=64)
ani
2. Usage Examples
Plotting a Field
fig, ax = visuals.field(touchdown_markings=False, fifty_yard=True)
plt.show()
Visualizing a Single Frame
fig, ax = visuals.snap(data, gameId=2022091200, playId=52, frameId=15, size=20)
plt.show()
Plotting a Field
ani = visuals.play_game(data, gameId=2022091200, playId=24, save=True, speed=150)
ani
3. Error Handling
- Data Validation: Functions check whether gameId, playId, and frameId exist in the data.
- Color Mapping: Ensures all club values map to valid colors. Raises an error if a club is unmapped.
- Saving Animations: Errors in saving are explained with links to relevant Matplotlib documentation.
4. Customization Options
- Player Marker Sizes: Control player marker sizes using the size parameter.
- Color Mapping: Customize player colors by passing a dictionary to club_colors.
- Animation Speed: Adjust frame transition time with the speed parameter.
- Output Formats: Save animations with customized parameters for the PillowWriter.
5. License
This project is licensed under the MIT License - see the LICENSE file for details.
This library is ideal for visualizing football plays with clear, customizable graphics and animations. For further assistance, refer to Matplotlib's documentation or provide specific details for troubleshooting.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file nfl_tracks-1.3.0.tar.gz
.
File metadata
- Download URL: nfl_tracks-1.3.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5fa97b28795f7ed753c6ee3d2d1ecfe34e460bfbd949ca0d041e3d2a303d9ce |
|
MD5 | 88dde74df3825af76486b747f5903de9 |
|
BLAKE2b-256 | a37f5991d5429f474a5049651f0d9ffaff1e1a0ff5910e0c0f6297d96b1c2b78 |
File details
Details for the file nfl_tracks-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: nfl_tracks-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7f5dcfab0e35c2788c5a55b8865dfe14e886d81c3bcf783bc42d0e5597068f1 |
|
MD5 | 0877769c53b3bf5e49f63bc78e878949 |
|
BLAKE2b-256 | 6cb8fdc292d5158370ac2ad5c1a6239a5a8a13b166358f9206c0df5229d4f1e8 |