Baseball
from sportypy.surfaces.baseball import BaseballField
BaseballField
A subclass of the BaseSurfacePlot class to make a baseball field.
This allows for the creation of the baseball field in a way that is entirely parameterized by the field's baseline characteristics. By convention, TV view for baseball is identical to the view from the high home plate camera.
All attributes should default to 0.0 (if of a numeric type) or an empty string (if of a string type). Customized parameters may be specified via a child class (see below) or by directly specifying all necessary attributes of a valid baseball field. The attributes needed to instantiate a particular league's surface must be specified in the field_params dictionary. For many leagues, these will be provided in the surface_dimensions.json file in the data/ subdirectory of sportypy.
See the BaseSurfacePlot and BaseSurface class definitions for full details.
Attributes
-
league_code(str): The league for which the plot should be drawn. This is case-insensitive but should be the shortened name of the league (e.g. "Major League Baseball" should be either "MLB" or "mlb"). The default is an empty string -
rotation_amt(float): The angle (in degrees) through which to rotate the final plot. The default is0.0 -
x_trans(float): The amount that thexcoordinates are to be shifted. By convention, the +xaxis extends from the pitcher's plate towards first base when viewing the field in TV view. The default is0.0 -
y_trans(float): The amount that theycoordinates are to be shifted. By convention, the +yaxis extends from the back tip of home plate out towards center field when viewing the field in TV view. The default is0.0 -
feature_colors(dict): A dictionary of coloring parameters to pass to the plot -
field_params(dict): A dictionary containing the following parameters of the field:field_units(str): The units of the fieldleft_field_distance(float): The straight-line distance from the back tip of home plate to the left field foul pole (along theta = -45°)right_field_distance(float): The straight-line distance from the back tip of home plate to the right field foul pole (along theta = +45°)center_field_distance(float): The straight-line distance from the back tip of home plate to straight-away center field (along theta = 0°)baseline_distance(float): The distance of each baselinerunning_lane_start_distance(float): The straight-line distance from the back tip of home plate to the start of the running lanerunning_lane_depth(float): The straight-line distance from the outer edge of the first-base line to the outer edge of the running lanerunning_lane_length(float): The straight-line length of the running lane measured from the point nearest home plate. As an example, if the base lines are 90 feet, and the running lane starts a distance of 45 feet down the line from the back tip of home plate, and extends 3 feet beyond first base, this parameter would be given as48.0pitchers_mound_center_to_home_plate(float): The distance from the center of the pitcher's mound to the back tip of home plate. NOTE: this does not necessarily align with the front edge of the pitcher's platepitchers_mound_radius(float): The radius of the pitcher's moundpitchers_plate_front_to_home_plate(float): The distance from the front edge of the pitcher's plate to the back tip of home platepitchers_plate_width(float): The width of the pitcher's plate (the dimension in theydirection)pitchers_plate_length(float): The length of the pitcher's plate (the dimension in thexdirection)base_side_length(float): The length of one side of a square basehome_plate_edge_length(float): The length of a full side of home plateinfield_arc_radius(float): The distance from the front edge of the pitcher's mound to the back of the infield dirtbase_anchor_to_infield_grass_radius(float): The distance from the anchor point of a base to the circular cutout in the infield grass. The anchor point of a base is defined as the point used in the definition of the base paths. As an example, in MLB, the anchor point of first base would be the corner of the first base bag along the foul line on the side furthest from the back tip of home plateline_width(float): The thickness of all chalk lines on the fieldfoul_line_to_infield_grass(float): The distance from the outer edge of the foul line to the outer edge of the infield grassfoul_line_to_foul_grass(float): The distance from the outer edge of the foul line to the inner edge of the grass in foul territorybatters_box_length(float): The length of the batter's box (in the y direction) measured from the outside of the chalk linesbatters_box_width(float): The width of the batter's box (in the x direction) measured from the outside of the chalk linesbatters_box_y_adj(float): The shift off of center in the y direction that the batter's box needs to be moved to properly alignhome_plate_side_to_batters_box(float): The distance from the outer edge of the batter's box to the outer edge of home platecatchers_box_shape(str): The shape of the catcher's box. Currently-supported values are:- "rectangle" (default behavior)
- "trapezoid" (see LittleLeagueField for example)
catchers_box_depth(float): The distance from the back tip of home plate to the back edge of the catcher's boxbackstop_radius(float): The distance from the back tip of home plate to the interior edge of the backstophome_plate_circle_radius(float): The radius of the dirt circle surrounding home plate
Leagues
Each class below is a BaseballField with that league's rule-book dimensions.
| Class | league_code | Description |
|---|---|---|
LittleLeagueField | little_league | A subclass of BaseballField specific to Little League. |
MiLBField | milb | A subclass of BaseballField specific to MiLB. |
MLBField | mlb | A subclass of BaseballField specific to MLB. |
NCAAField | ncaa | A subclass of BaseballField specific to NCAA baseball. |
NFHSField | nfhs | A subclass of BaseballField specific to NFHS (high school baseball). |
PonyField | pony | A subclass of BaseballField specific to Pony League Baseball. |
Methods
draw
BaseballField.draw(ax = None, display_range = 'full', xlim = None, ylim = None, rotation = None)
Draw the field.
Parameters
-
ax(matplotlib.Axes): An Axes object onto which the plot can be drawn. IfNoneis supplied, then the currently-active Axes object will be used -
display_range(str): The portion of the surface to display. The entire surface will always be drawn under the hood, however this parameter limits what is shown in the final plot. The following explain what each display range corresponds to:"full": The entire surface"infield": The infield portion of the baseball diamond
The default is
"full" -
xlim(float or tuple of floats or None): The display range in thexdirection to be used. If a single float is provided, this will be used as the lower bound of thexcoordinates to display and the upper bound will be the +xend of the field. If a tuple, the two values will be used to determine the bounds. IfNone, then thedisplay_rangewill be used instead to set the bounds. The default isNone -
ylim(float or tuple of floats or None): The display range in theydirection to be used. If a single float is provided, this will be used as the lower bound of theycoordinates to display and the upper bound will be the +yend of the field. If a tuple, the two values will be used to determine the bounds. IfNone, then thedisplay_rangewill be used instead to set the bounds. The default is `None`` -
rotation(float or None): Angle (in degrees) through which to rotate the field when drawing. If used, this will set the class attribute of_rotation. A value of0.0will correspond to a TV view of the field, where +xis to the right and +yis on top. The rotation occurs counter clockwise
cani_plot_leagues
BaseballField.cani_plot_leagues(league_code = None)
Show if a league can be plotted, or what leagues are pre-defined.
A user may wish to know if a specific baseball league can be plotted. This method allows a user to check if that specific league code comes shipped with sportypy for easier plotting (if they provide the league code), or can also show what leagues are available to be plotted
Parameters
league_code(str or None): A league code that may or may not be shipped with the package. If the league code isNone, this will display all leagues that do come shipped withsportypy11. The default isNone``
Returns
Nothing, but a message will be printed out:
cani_color_features
BaseballField.cani_color_features()
Determine what features of the field can be colored.
This function is a helper function for the user to aid in plot styling and customization. The printed result of this method will be the names of the features that are able to be colored
Returns
Nothing, but a message will be printed out:
cani_change_dimensions
BaseballField.cani_change_dimensions()
Determine what features of the field can be re-parameterized.
This function is a helper function for the user to aid in customizing a field's parameters. The printed result of this method will be the names of the features that are able to be reparameterized. This method is also useful when defining new features and using an existing league's field dimensions as a starting point
Returns
Nothing, but a message will be printed out:
update_colors
BaseballField.update_colors(color_updates = {}, *args, **kwargs)
Update the colors currently used in the plot.
The colors can be passed at the initial instantiation of the class via the color_updates parameter, but this method allows the colors to be updated after the initial instantiation and will re-instantiate the class with the new colors
Parameters
color_updates(dict): A dictionary where the keys correspond to the name of the feature that's color is to be updated (seecani_color_features()method for a list of these names). The default is an empty dictionary
Returns
Nothing, but the class is re-instantiated with the updated colors:
update_field_params
BaseballField.update_field_params(field_param_updates = {}, *args, **kwargs)
Update the field's defining parameters.
This method should primarily be used in cases when plotting a league not currently supported by sportypy
Parameters
field_updates(dict): A dictionary where the keys correspond to the name of the parameter of the field that is to be updated (seecani_change_dimensions()method for a list of these parameters). The default is an empty dictionary
Returns
Nothing, but the class is re-instantiated with the updated parameters:
reset_colors
BaseballField.reset_colors()
Reset the features of the field to their default color set.
The colors can be passed at the initial instantiation of the class via the color_updates parameter, and through the update_colors() method, these can be changed. This method allows the colors to be reset to their default values after experiencing such a change
reset_field_params
BaseballField.reset_field_params()
Reset the features of the field to their default parameterizations.
The field parameters can be passed at the initial instantiation of the class via the field_updates parameter, and through the update_field_params() method, these can be changed. This method allows the feature parameterization to be reset to their default values after experiencing such a change