{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This kernel generates a structure of a BaseBall field in **2-D and 3-D Visualization**. The structure is created using **Matplotlib** where the function returns **(fig,ax)** which can be used to plot event data(probably). Happy Plotting!!!\n\n\nIf you like my work, feel free to visit **Games data/PlayerBox scores** visualisation in following link:\n* https://www.kaggle.com/debojit23/mlb-eda-1-games-data-playerboxscores","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Circle, PathPatch\nfrom mpl_toolkits.mplot3d import Axes3D \nimport mpl_toolkits.mplot3d.art3d as art3d\nimport matplotlib as mpl\nimport numpy as np \nimport pandas as pd ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define field class","metadata":{}},{"cell_type":"code","source":"names = ['Catcher','HP','Pitcher','CF']\ncoord = [(0.06, 0.08),(0.11, 0.12),(0.265, 0.265),(0.65, 0.65)]\ncolor = ['blue','brown','blue','brown']\nrot = [135,0,135,9,45,0,135]\nfontsize=[12,12,12,10,12,12,]\nclass Baseball_Field:\n    def __init__(self,figsize):\n        self.figsize=figsize\n\n    def __generate_baseball_field2D__(self):\n\n        names = ['Catcher','Home Plate','1st Base','2nd Base','3rd Base','Pitcher','Outfield','Infield','Coach Box1','Coach Box2','ShortStop','Left Fielder','Center Fielder','Right Fielder']\n        coord = [(0.06, 0.08),(0.11, 0.12),(0.43, 0.12),(0.35, 0.43),(0.11, 0.43),(0.265, 0.265),(0.6, 0.6),(0.41, 0.41),(0.07, 0.39),(0.39, 0.07),(0.25, 0.46),(0.3, 0.8),(0.55, 0.55),(0.8, 0.3)]\n        color = ['white','gold','gold','gold','gold','white','gold','gold','gold','gold','gold','white','white','white']\n        rot = [135,0,0,0,0,135,135,135,90,0,0,150,135,110]\n        fontsize=[12,12,12,12,12,12,20,20,10,10,12,12,12,12]\n\n        fig = plt.figure()\n        # add a wedge\n        fig, ax = plt.subplots(1, figsize=self.figsize)\n        ax.add_patch(patches.Rectangle((0.0,0.0), 1, 1, facecolor=\"#247309\",  alpha=0.50))\n\n        ax.add_patch(patches.Wedge((0.05,0.05), 0.89, 360, 90, ec=\"none\",edgecolor='orange',facecolor=\"green\", linewidth=2, ))\n        ax.add_patch(patches.Wedge((0.1,0.1), 0.52, 360, 90, ec=\"none\",edgecolor='orange',facecolor='white', linewidth=2, ))\n        ax.add_patch(patches.Wedge((0.1,0.1), 0.51, 360, 90, ec=\"none\",edgecolor='orange',facecolor='#66431d', linewidth=2, ))\n        ax.add_patch(patches.Rectangle((0.1,0.1), 0.32, 0.32, facecolor=\"white\",ec=\"none\"))\n        ax.add_patch(patches.Rectangle((0.1,0.1), 0.31, 0.31, facecolor=\"green\",ec=\"none\"))\n\n        ax.add_patch(patches.Circle((0.1,0.1), 0.056, ec=\"none\",facecolor='white',edgecolor='white',linewidth=10))\n\n        ax.add_patch(patches.Circle((0.1,0.1), 0.05, ec=\"none\",facecolor='#66431d',edgecolor='white',linewidth=10))\n        ax.add_patch(patches.Wedge((0.1,0.1), 0.056, 360, 90, ec=\"none\",facecolor=\"#66431d\" ))\n        ax.add_patch(patches.Rectangle((0.1,0.1), 1.0, 0.01, facecolor=\"white\",ec=\"none\"))\n        ax.add_patch(patches.Rectangle((0.1,0.1), 0.01, 1, facecolor=\"white\",ec=\"none\"))\n        ax.add_patch(patches.Circle((0.26,0.26), 0.02, ec=\"none\",facecolor='white',edgecolor='white',linewidth=10))\n\n\n\n        for i,name in enumerate(names):\n            plt.text(coord[i][0],coord[i][1], names[i], color=color[i],rotation=rot[i],fontsize=fontsize[i])\n\n        plt.grid()\n        return fig,ax\n\n    def __generate_baseball_field3D__(self):\n\n        fig = plt.figure(figsize=self.figsize)\n        ax=fig.gca(projection='3d')\n\n        #for i in [\"x\",\"y\",\"z\"]:\n\n        rect1 = patches.Rectangle((0.0,0.0), 1, 1, facecolor=\"#247309\",  alpha=0.1)\n        ax.add_patch(rect1)\n        art3d.pathpatch_2d_to_3d(rect1, z=0, zdir=\"z\",)\n\n        wedge1 = patches.Wedge((0.05,0.05), 0.89, 360, 90,alpha=0.1,facecolor=\"green\", linewidth=2, )\n        ax.add_patch(wedge1)\n        art3d.pathpatch_2d_to_3d(wedge1, z=0, zdir=\"z\")\n\n        wedge2 = patches.Wedge((0.1,0.1), 0.52, 360, 90, alpha=0.2,facecolor='#66431d', linewidth=2, )\n        ax.add_patch(wedge2)\n        art3d.pathpatch_2d_to_3d(wedge2, z=0, zdir=\"z\")\n\n        rect2 = patches.Rectangle((0.1,0.1), 0.32, 0.32,alpha=0.1, facecolor=\"white\")\n        ax.add_patch(rect2)\n        art3d.pathpatch_2d_to_3d(rect2, z=0, zdir=\"z\")\n\n        rect3 = patches.Rectangle((0.1,0.1), 0.31, 0.31, alpha=0.2, facecolor=\"green\")\n        ax.add_patch(rect3)\n        art3d.pathpatch_2d_to_3d(rect3, z=0, zdir=\"z\")\n\n        circle1 = patches.Circle((0.1,0.1), 0.056,alpha=0.1,facecolor='white')\n        ax.add_patch(circle1)\n        art3d.pathpatch_2d_to_3d(circle1, z=0, zdir=\"z\")\n\n        circle2 = patches.Circle((0.1,0.1), 0.05,alpha=0.1,facecolor='#66431d')\n        ax.add_patch(circle2)\n        art3d.pathpatch_2d_to_3d(circle2, z=0, zdir=\"z\")\n\n        wedge4 = patches.Wedge((0.1,0.1), 0.056, 360, 90,alpha=1,facecolor=\"#66431d\" )\n        ax.add_patch(wedge4)\n        art3d.pathpatch_2d_to_3d(wedge4, z=0, zdir=\"z\")\n\n        rect4 = patches.Rectangle((0.1,0.1), 0.89, 0.01, facecolor=\"white\",alpha=1)\n        ax.add_patch(rect4)\n        art3d.pathpatch_2d_to_3d(rect4, z=0, zdir=\"z\")\n\n\n        rect5 = patches.Rectangle((0.1,0.1), 0.01, 0.89, facecolor=\"white\",alpha=1)\n        ax.add_patch(rect5)\n        art3d.pathpatch_2d_to_3d(rect5, z=0, zdir=\"z\")\n\n        circle3 = patches.Circle((0.26,0.26), 0.02, facecolor='white',alpha=1)\n        ax.add_patch(circle3)\n        art3d.pathpatch_2d_to_3d(circle3, z=0, zdir=\"z\")\n\n        \n        for J,name in enumerate(names):\n            ax.text(coord[J][0],coord[J][1], 0, names[J], color=color[J],rotation=rot[J],\n                    fontsize=fontsize[J],zdir=\"z\",verticalalignment='center')\n                #plt.text(coord[i][0],coord[i][1], names[i], color=color[i],rotation=rot[i],fontsize=fontsize[i])\n        ax.text(0.4,0.004,0,\"Coach Box\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.004,0.4,0,\"Coach Box\",color='Brown',fontsize=12,zdir=(0,1,0),verticalalignment='center')\n        ax.text(0.12,0.43,0,\"B3\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.35, 0.43,0,\"B2\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.43, 0.12,0,\"B1\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.25, 0.46,0,\"SS\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.8, 0.3,0,\"RF\",color='Brown',fontsize=12,zdir=(0,1,0),verticalalignment='center')\n        ax.text(0.3, 0.8,0,\"LF\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.95,\"LF : Left Fielder\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.9,\"CF : Central Fielder\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.85,\"RF : Right Fielder\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.8,\"SS : ShortStaff\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.75,\"B1 : First Baseman\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.70,\"B2 : Second Baseman\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n        ax.text(0.2, 1,0.65,\"B3 : Third Baseman\",color='Brown',fontsize=12,zdir=(1,0,0),verticalalignment='center')\n\n        ax.set_xlabel('X axis')\n        ax.set_ylabel('Y axis')\n        ax.set_zlabel('Z axis')\n        ax.set_xlim3d(0, 1)\n        ax.set_ylim3d(0, 1)\n        ax.set_zlim3d(0, 1)\n\n        return fig,ax\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T16:07:18.006068Z","iopub.execute_input":"2021-06-27T16:07:18.006456Z","iopub.status.idle":"2021-06-27T16:07:18.059571Z","shell.execute_reply.started":"2021-06-27T16:07:18.006421Z","shell.execute_reply":"2021-06-27T16:07:18.058421Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generate 3-D field[allowed figsize=(14,14),(16,16)->best,(18,18))]","metadata":{}},{"cell_type":"code","source":"obj=Baseball_Field((16,16))\nfig,ax=obj.__generate_baseball_field3D__()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T16:06:10.548887Z","iopub.execute_input":"2021-06-27T16:06:10.549287Z","iopub.status.idle":"2021-06-27T16:06:10.934264Z","shell.execute_reply.started":"2021-06-27T16:06:10.549254Z","shell.execute_reply":"2021-06-27T16:06:10.932959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generate 2d field[allowed figsize=(12,12),(14,14)->best,(16,16))]","metadata":{}},{"cell_type":"code","source":"\nobj=Baseball_Field((14,14))\nfig,ax=obj.__generate_baseball_field2D__()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T16:06:56.608311Z","iopub.execute_input":"2021-06-27T16:06:56.608728Z","iopub.status.idle":"2021-06-27T16:06:56.949574Z","shell.execute_reply.started":"2021-06-27T16:06:56.608694Z","shell.execute_reply":"2021-06-27T16:06:56.948371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}