import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def visualize(dataframe, balltype):
    df = dataframe

    #Filter by balltype
    res = df[df["pitch_type"] == balltype]

    #Group by results
    groups = res.groupby("description")
    for name, group in groups:
        if name == "miss":
            plt.plot(group["plate_x"], group["plate_z"], marker="o", linestyle="", color="none", ms = 3, mec="#9A9A9A", label=name)
        else:
            plt.plot(group["plate_x"], group["plate_z"], marker="o", linestyle="", color="none", ms = 3, mec="#03A77F", label=name)


    #Fixing the viewpoint of the plot
    axes = plt.gca()
    axes.set_xlim([-2.50,2.50])
    axes.set_ylim([0.00,5.00])

    #Setting strike zone
    sz_top_avg = res["sz_top"].mean()
    sz_bottom_avg = res["sz_bot"].mean()
    sz_left = -0.85
    sz_right = 0.85

    #Drawing strike zone
    plt.plot((sz_left, sz_right), (sz_top_avg, sz_top_avg), 'k-')
    plt.plot((sz_left, sz_right), (sz_bottom_avg, sz_bottom_avg), 'k-')
    plt.plot((sz_left, sz_left), (sz_top_avg, sz_bottom_avg), 'k-')
    plt.plot((sz_right, sz_right), (sz_top_avg, sz_bottom_avg), 'k-')

    #Setting labels
    plt.xlabel("Horizontal Location")
    plt.ylabel("Vertical Location")
    plt.title(f"{player_name} 2018\n {ballname_dict.get(balltype, balltype)}")

    plt.legend()
    plt.show()


#Setting up Name and CSV location
player_name = "Put player name"
file_src = "Put target csv"

raw = pd.read_csv(file_src)
df = pd.DataFrame(raw)

#For filtering cases
replace_dict = {"description": {"hit_into_play_no_out": "contact", "hit_into_play": "contact", "hit_into_play_score": "contact", "swinging_strike": "miss", "swinging_strike_blocked": "miss"}}
ballname_dict = {"FF": "4-Seam Fastball", "CH": "Changeup", "CU": "Curveball", "SL": "Slider", "FT": "2-Seam Fastball", "AB": "Automatic Ball",
                "AS": "Automatic Strike", "EP": "Eephus", "FC": "Cutter", "FO": "Forkball", "FS": "Splitter", "GY": "Gyroball", "IN": "Intentional Ball",
                "KC": "Knuckle Curve", "NP": "No Pitch", "PO": "Pitchout", "SC": "Screwball", "SI": "Sinker", "UN": "Unknown"}

df = df.replace(replace_dict)
df = df[df["description"].isin(["contact", "miss"])]
for i in df["pitch_type"].unique():
    visualize(df, i)

[((50, 6, 50, 27), 'pandas.read_csv', 'pd.read_csv', ({(50, 18, 50, 26): 'file_src'}, {}), '(file_src)', True, 'import pandas as pd\n'), ((51, 5, 51, 22), 'pandas.DataFrame', 'pd.DataFrame', ({(51, 18, 51, 21): 'raw'}, {}), '(raw)', True, 'import pandas as pd\n'), ((21, 11, 21, 20), 'matplotlib.pyplot.gca', 'plt.gca', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((32, 4, 32, 65), 'matplotlib.pyplot.plot', 'plt.plot', ({(32, 13, 32, 32): '(sz_left, sz_right)', (32, 34, 32, 58): '(sz_top_avg, sz_top_avg)', (32, 60, 32, 64): '"""k-"""'}, {}), "((sz_left, sz_right), (sz_top_avg, sz_top_avg), 'k-')", True, 'import matplotlib.pyplot as plt\n'), ((33, 4, 33, 71), 'matplotlib.pyplot.plot', 'plt.plot', ({(33, 13, 33, 32): '(sz_left, sz_right)', (33, 34, 33, 64): '(sz_bottom_avg, sz_bottom_avg)', (33, 66, 33, 70): '"""k-"""'}, {}), "((sz_left, sz_right), (sz_bottom_avg, sz_bottom_avg), 'k-')", True, 'import matplotlib.pyplot as plt\n'), ((34, 4, 34, 67), 'matplotlib.pyplot.plot', 'plt.plot', ({(34, 13, 34, 31): '(sz_left, sz_left)', (34, 33, 34, 60): '(sz_top_avg, sz_bottom_avg)', (34, 62, 34, 66): '"""k-"""'}, {}), "((sz_left, sz_left), (sz_top_avg, sz_bottom_avg), 'k-')", True, 'import matplotlib.pyplot as plt\n'), ((35, 4, 35, 69), 'matplotlib.pyplot.plot', 'plt.plot', ({(35, 13, 35, 33): '(sz_right, sz_right)', (35, 35, 35, 62): '(sz_top_avg, sz_bottom_avg)', (35, 64, 35, 68): '"""k-"""'}, {}), "((sz_right, sz_right), (sz_top_avg, sz_bottom_avg), 'k-')", True, 'import matplotlib.pyplot as plt\n'), ((38, 4, 38, 37), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(38, 15, 38, 36): '"""Horizontal Location"""'}, {}), "('Horizontal Location')", True, 'import matplotlib.pyplot as plt\n'), ((39, 4, 39, 35), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(39, 15, 39, 34): '"""Vertical Location"""'}, {}), "('Vertical Location')", True, 'import matplotlib.pyplot as plt\n'), ((42, 4, 42, 16), 'matplotlib.pyplot.legend', 'plt.legend', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((43, 4, 43, 14), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((15, 12, 15, 131), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n'), ((17, 12, 17, 131), 'matplotlib.pyplot.plot', 'plt.plot', (), '', True, 'import matplotlib.pyplot as plt\n')]