Skip to main content

A library to plot various sports fields using Matplotlib

Project description

mplsports (WIP)

A highly customizable library for plotting sports fields and courts using Matplotlib.

Work in progress! Still doesn't work yet!


Table of Contents


Overview

mplsports is a Python library designed for plotting various sports fields and courts using Matplotlib. It provides high customizability, allowing users to adjust dimensions, colors, line styles, and more. The library supports multiple sports, including soccer, basketball, tennis, handball, and volleyball, with an extensible architecture that makes it easy to add new sports.


Features

  • Multiple Sports Support: Plot pitches for soccer, basketball, tennis, handball, volleyball, and more.
  • High Customizability: Customize colors, line styles, dimensions, units, and padding.
  • Extensible Architecture: Easily add support for additional sports.
  • Standard and Custom Dimensions: Use standard dimensions or provide your own.
  • Future-Proof: Designed to support both 2D and upcoming 3D plotting capabilities.

Installation

Install mplsports using pip:

pip install mplsports

Quick Start

mplsports provides a flexible API that accommodates various terms for sports playing areas. You can use Pitch, Court, Field, Ground, or even Thing interchangeably to create your sports visualizations.  This flexibility allows you to use terminology you're comfortable with while maintaining full access to the library's features.

from mplsports import Pitch, Court, Field, Ground, Thing
import matplotlib.pyplot as plt

# Create a 2x2 grid of subplots
fig, axs = plt.subplots(2, 2, figsize=(20, 20))
Flatten the 2D array of axes for easier indexing
axs = axs.flatten()

# soccer
soccer_pitch = Pitch(sport="soccer", axis=True, tick=True)
soccer_pitch.draw(ax=axs[0])
axs[0].set_title("Soccer Pitch")

# Basketball
basketball_court = Court(sport="basketball", axis=True, tick=True)
basketball_court.draw(ax=axs[1])
axs[1].set_title("Basketball Court")

# Tennis
tennis_court = Field(sport="tennis", axis=True, tick=True)
tennis_court.draw(ax=axs[2])
axs[2].set_title("Tennis Court")

# Handball
handball_court = Ground(sport="handball", axis=True, tick=True)
handball_court.draw(ax=axs[3])
axs[3].set_title("Handball Court")

# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

Plotting a Soccer Pitch

from mplsports.pitch.soccer import SoccerPitch
import matplotlib.pyplot as plt

# Create a soccer pitch instance
pitch = SoccerPitch(
    pitch_color='#a8bc95',
    line_color='white',
    line_alpha=0.8,
    linewidth=2,
    linestyle='-',
    axis=True,
    tick=False,
    label=False
)

# Draw the pitch
ax = pitch.draw()

# Show the plot
plt.show()

Plotting Other Sports

from mplsports.pitch.basketball import BasketballCourt

# Create a basketball court instance
court = BasketballCourt(
    pitch_color='#f0f0f0',
    line_color='black',
    linewidth=2,
    half=False
)

# Draw the court
ax = court.draw()

plt.show()

Customization

mplsports allows extensive customization through various parameters:

  • Colors: pitch_color, line_color
  • Line Styles: linewidth, linestyle, line_alpha, line_zorder
  • Axis Settings: axis, tick, label
  • Padding: pad_left, pad_right, pad_bottom, pad_top
  • Dimension Configurations: Use predefined pitch types or pass custom dimensions.

Example: Customizing a Tennis Court

from mplsports.pitch.tennis import TennisCourt

court = TennisCourt(
    pitch_color='#008000',
    line_color='white',
    linewidth=2,
    line_zorder=1,
    axis=True,
    tick=False,
    label=False,
    pad_left=2,
    pad_right=2,
    pad_bottom=1,
    pad_top=1
)

ax = court.draw()
plt.show()

Adding Custom Dimensions

You can use custom dimensions by creating a dictionary of dimensions or providing a custom YAML file.

Using a Custom Dimensions Dictionary

custom_dimensions = {
    'length': 120.0,
    'width': 75.0,
    'line_width': 0.12,
    'penalty_area_length': 18.0,
    'penalty_area_width': 44.0,
    'goal_area_length': 6.0,
    'goal_area_width': 20.0,
    'center_circle_radius': 10.0,
    'corner_arc_radius': 1.0
}

pitch = SoccerPitch(config=custom_dimensions)
ax = pitch.draw()
plt.show()

Using a Custom YAML Configuration

  1. Create a YAML file with your custom dimensions (e.g., my_custom_pitch.yaml).
length: 120.0
width: 75.0
line_width: 0.12
penalty_area_length: 18.0
penalty_area_width: 44.0
goal_area_length: 6.0
goal_area_width: 20.0
center_circle_radius: 10.0
corner_arc_radius: 1.0
  1. Load the configuration and pass it to the pitch class.
import yaml

with open('my_custom_pitch.yaml', 'r') as f:
    custom_config = yaml.safe_load(f)

pitch = SoccerPitch(config=custom_config)
ax = pitch.draw()
plt.show()

Roadmap

  • Verify pitches for each sport:
    • Soccer
    • Basketball
    • Tennis
    • Handball
    • Volleyball
  • Add documentation
  • Add 3D plots for each sport:
    • Soccer
    • Basketball
    • Tennis
    • Handball
    • Volleyball

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository on GitHub.
  2. Create a new branch for your feature or bugfix.
  3. Write tests for your changes.
  4. Submit a pull request with a detailed description.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Acknowledgements

Special thanks to the community for their contributions and support. This project was heavily influenced by:

I initially struggled to plot a handball court for my research and thought it would be useful for myself and colleagues to have a simple, yet extensible version of these two repositories.

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

mplsports-0.2.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

mplsports-0.2.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file mplsports-0.2.0.tar.gz.

File metadata

  • Download URL: mplsports-0.2.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.17

File hashes

Hashes for mplsports-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9af90629ab687d3d38149151c3f8e10b5fdc6d692a73be3a35171257e1c86452
MD5 993e8b97f262fbc14cc73fcbdb56d684
BLAKE2b-256 9f993a1ffadc1e3d528c5e0c99ca3e334a7a1aea27aae2ef5879c2ff1db4f7d4

See more details on using hashes here.

File details

Details for the file mplsports-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mplsports-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.17

File hashes

Hashes for mplsports-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3231750b4a1b8c397c263f28131e4c9f43daf379f68bca3c885ddee1dda8876
MD5 d7c4b6f97cf9e1ee777ce1104da4b0d5
BLAKE2b-256 93121309b2f6b3232242b19b648a3d9a35e9e51f8f141db11673f9b186845357

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page