【メモ】大谷選手(2021年)の打撃データ / 打球方向&コースごと / Code

昨日投稿のデータのコード備忘録

*   記事<br>[大谷選手(2021年)の打撃データ / 打球方向&コースごと](https://hide.ac/articles/TSt5Pgkgt)

-------

# 打球方向

<pre><code>!pip install pybaseball
from pybaseball import statcast
df = statcast(start_dt='2021-04-01', end_dt='2021-12-31')
df_ohtani = df[df['batter'] == 660271]</code></pre>

> 参考:大谷さんのID → <https://baseballsavant.mlb.com/savant-player/shohei-ohtani-660271>

<pre><code>df_ohtani</code></pre>

![](https://pbs.twimg.com/media/FmZ6vtWakAEEBLf?format=png&name=900x900)

## カラム確認

<pre><code>df_ohtani.columns</code></pre>

<pre><code>Index(['pitch_type', 'game_date', 'release_speed', 'release_pos_x',
       'release_pos_z', 'player_name', 'batter', 'pitcher', 'events',
       'description', 'spin_dir', 'spin_rate_deprecated',
       'break_angle_deprecated', 'break_length_deprecated', 'zone', 'des',
       'game_type', 'stand', 'p_throws', 'home_team', 'away_team', 'type',
       'hit_location', 'bb_type', 'balls', 'strikes', 'game_year', 'pfx_x',
       'pfx_z', 'plate_x', 'plate_z', 'on_3b', 'on_2b', 'on_1b',
       'outs_when_up', 'inning', 'inning_topbot', 'hc_x', 'hc_y',
       'tfs_deprecated', 'tfs_zulu_deprecated', 'fielder_2', 'umpire', 'sv_id',
       'vx0', 'vy0', 'vz0', 'ax', 'ay', 'az', 'sz_top', 'sz_bot',
       'hit_distance_sc', 'launch_speed', 'launch_angle', 'effective_speed',
       'release_spin_rate', 'release_extension', 'game_pk', 'pitcher.1',
       'fielder_2.1', 'fielder_3', 'fielder_4', 'fielder_5', 'fielder_6',
       'fielder_7', 'fielder_8', 'fielder_9', 'release_pos_y',
       'estimated_ba_using_speedangle', 'estimated_woba_using_speedangle',
       'woba_value', 'woba_denom', 'babip_value', 'iso_value',
       'launch_speed_angle', 'at_bat_number', 'pitch_number', 'pitch_name',
       'home_score', 'away_score', 'bat_score', 'fld_score', 'post_away_score',
       'post_home_score', 'post_bat_score', 'post_fld_score',
       'if_fielding_alignment', 'of_fielding_alignment', 'spin_axis',
       'delta_home_win_exp', 'delta_run_exp'],
      dtype='object')</code></pre>



## データ確認

<pre><code>df_ohtani[['hit_distance_sc', 'launch_angle']]</code></pre>

![](https://pbs.twimg.com/media/FmZ7pcnaYAArcoT?format=png&name=small)

<pre><code>df_ohtani['events'].unique()</code></pre>

<pre><code>array(['strikeout', nan, 'home_run', 'walk', 'field_out', 'double',
       'triple', 'single', 'pickoff_3b', 'force_out', 'hit_by_pitch',
       'grounded_into_double_play', 'field_error', 'sac_fly',
       'fielders_choice', 'strikeout_double_play'], dtype=object)</code></pre>

## ホームラン

<pre><code>df_ohtani[df_ohtani['events'] == 'home_run']</code></pre>

![](https://pbs.twimg.com/media/FmZ8TPDaMAM8xmI?format=png&name=small)

<pre><code>df_ohtani[df_ohtani['events'] == 'home_run'][['hit_distance_sc', 'launch_angle', 'launch_speed', 'events', 'description','hc_x', 'hc_y']]</code></pre>

![](https://pbs.twimg.com/media/FmZ8gT6acAMRoKn?format=png&name=small)

## ホームランplot

<pre><code># 全部
import matplotlib.pyplot as plt
df_home_run = df[df['events'] == 'home_run']
df_home_run = df_home_run.dropna(subset=['hc_x','hc_y'])
plt.scatter(df_home_run['hc_x'], df_home_run['hc_y'])
plt.xlim(-10, 270)
plt.ylim(-50, 220)
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210, 85], 'k-', lw=3)

plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ8x4HaUAMjCgu?format=png&name=small)


<pre><code>df_home_run_660271 = df_ohtani[(df_ohtani['events'] == 'home_run') & (df_ohtani['batter'] == 660271)]
df_home_run = df_home_run.dropna(subset=['hc_x','hc_y'])
plt.scatter(df_home_run['hc_x'], df_home_run['hc_y'], color='gray')
plt.scatter(df_home_run_660271['hc_x'], df_home_run_660271['hc_y'], color='r')

plt.xlim(-10, 270)
plt.ylim(-50, 220)


plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210, 85], 'k-', lw=3)

plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ88CcaUAIF2C-?format=png&name=small)

## 打球plot

<pre><code>import matplotlib.pyplot as plt
df = df.dropna(subset=['hc_x','hc_y'])
plt.scatter(df['hc_x'], df['hc_y'])
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210, 85], 'k-', lw=3)

plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ9NFeakAEkjmA?format=png&name=small)

<pre><code># batter 660271の結果を取り出し、df_660271に格納
df_660271 = df[df["batter"] == 660271]

# 全体をプロット
plt.scatter(df['hc_x'], df['hc_y'], color='gray')

# batter 660271の結果を赤色でプロット
plt.scatter(df_660271['hc_x'], df_660271['hc_y'], color='red')

plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210, 85], 'k-', lw=3)

plt.show()

</code></pre>

![](https://pbs.twimg.com/media/FmZ9U6UaAAA-4_q?format=png&name=small)

## 大谷のみ

<pre><code>import matplotlib.pyplot as plt
df_batter_660271 = df[df['batter']==660271]

home_run_events = ['home_run', 'double', 'triple', 'single', 'hit_by_pitch']

df_batter_660271_home_run = df_batter_660271[df_batter_660271['events'].isin(home_run_events)]
plt.scatter(df_batter_660271_home_run['hc_x'], df_batter_660271_home_run['hc_y'], color='red')

df_batter_660271_others = df_batter_660271[~df_batter_660271['events'].isin(home_run_events)]
plt.scatter(df_batter_660271_others['hc_x'], df_batter_660271_others['hc_y'], color='gray')

plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210, 85], 'k-', lw=3)

plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ9hb8aUAAE1f_?format=png&name=small)


<pre><code>import matplotlib.pyplot as plt

#home_runのデータを取り出す
df_hr = df[df['events'] == 'home_run']
df_hr = df_hr[df_hr['batter'] == 660271]
#doubleのデータを取り出す
df_2b = df[df['events'] == 'double']
df_2b = df_2b[df_2b['batter'] == 660271]
#tripleのデータを取り出す
df_3b = df[df['events'] == 'triple']
df_3b = df_3b[df_3b['batter'] == 660271]
#singleのデータを取り出す
df_1b = df[df['events'] == 'single']
df_1b = df_1b[df_1b['batter'] == 660271]
#hit_by_pitchのデータを取り出す
df_hbp = df[df['events'] == 'hit_by_pitch']
df_hbp = df_hbp[df_hbp['batter'] == 660271]

#outイベントのデータを取り出す
df_out = df[~df['events'].isin(['home_run', 'double', 'triple', 'single', 'hit_by_pitch'])]
df_out = df_out[df_out['batter'] == 660271]

#plotする
plt.scatter(df_hr['hc_x'], df_hr['hc_y'], color='r', label='home_run')
plt.scatter(df_2b['hc_x'], df_2b['hc_y'], color='b', label='double')
plt.scatter(df_3b['hc_x'], df_3b['hc_y'], color='g', label='triple')
plt.scatter(df_1b['hc_x'], df_1b['hc_y'], color='y', label='single')
# plt.scatter(df_hbp['hc_x'], df_hbp['hc_y'], color='c', label='hit_by_pitch')
plt.scatter(df_out['hc_x'], df_out['hc_y'], color='gray', label='out', zorder=0)
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.gca().invert_yaxis()
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)

# フェア線を引く
plt.plot([125, 250], [210, 85], 'k-', lw=3)
plt.plot([125, 0], [210,85], 'k-', lw=3)



plt.title("Ohtani, direction of the batted ball 2021")

plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ-CEHaEAIgxz4?format=png&name=small)

-------


# コースごとの結果

## 大谷選手 全球をplot

<pre><code>import matplotlib.pyplot as plt

# create scatter plot using 'plate_x' and 'plate_z' columns
df_ohtani = df_ohtani.dropna(subset=['plate_x','plate_z'])
plt.scatter(df_ohtani['plate_x'], df_ohtani['plate_z'])

# add labels for x and y axis
plt.xlabel('Plate X')
plt.ylabel('Plate Z')

# show the plot
plt.show()
</code></pre>

## ヒット&デッドボール(hit by pitch)

<pre><code>

#home_runのデータを取り出す
df_hr = df[df['events'] == 'home_run']
df_hr = df_hr[df_hr['batter'] == 660271]
#doubleのデータを取り出す
df_2b = df[df['events'] == 'double']
df_2b = df_2b[df_2b['batter'] == 660271]
#tripleのデータを取り出す
df_3b = df[df['events'] == 'triple']
df_3b = df_3b[df_3b['batter'] == 660271]
#singleのデータを取り出す
df_1b = df[df['events'] == 'single']
df_1b = df_1b[df_1b['batter'] == 660271]
#hit_bypitchのデータを取り出す
df_hbp = df[df['events'] == 'hit_by_pitch']
df_hbp = df_hbp[df_hbp['batter'] == 660271]

#outイベントのデータを取り出す
df_out = df[~df['events'].isin(['home_run', 'double', 'triple', 'single', 'hit_by_pitch'])]
df_out = df_out[df_out['batter'] == 660271]

df_1b = df_1b.dropna(subset=['plate_x','plate_z'])
df_hr = df_hr.dropna(subset=['plate_x','plate_z'])
df_2b = df_2b.dropna(subset=['plate_x','plate_z'])
df_3b = df_3b.dropna(subset=['plate_x','plate_z'])
df_hbp = df_hbp.dropna(subset=['plate_x','plate_z'])
df_out = df_out.dropna(subset=['plate_x','plate_z'])


#create scatter plot using 'plate_x' and 'plate_z' columns
plt.scatter(df_hr['plate_x'], df_hr['plate_z'], color='r', label='home_run')
plt.scatter(df_2b['plate_x'], df_2b['plate_z'], color='g', label='double')
plt.scatter(df_3b['plate_x'], df_3b['plate_z'], color='b', label='triple')
plt.scatter(df_1b['plate_x'], df_1b['plate_z'], color='c', label='single')
plt.scatter(df_hbp['plate_x'], df_hbp['plate_z'], color='m', label='hit_by_pitch')

# plt.scatter(df_out['plate_x'], df_out['plate_z'], color='gray', label='out', marker='.')

# add labels for x and y axis
plt.xlabel('Plate X')
plt.ylabel('Plate Z')

# add legend
plt.legend()

# show the plot
plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ_QDHacAApOLi?format=png&name=small)

## event確認

<pre><code>df['events'].value_counts()
</code></pre>

![](https://pbs.twimg.com/media/FmZ_khSakAYkr2A?format=png&name=small)

## ヒット&デッドボール以外をoutとしてplot

<pre><code>

#home_runのデータを取り出す
df_hr = df[df['events'] == 'home_run']
df_hr = df_hr[df_hr['batter'] == 660271]
#doubleのデータを取り出す
df_2b = df[df['events'] == 'double']
df_2b = df_2b[df_2b['batter'] == 660271]
#tripleのデータを取り出す
df_3b = df[df['events'] == 'triple']
df_3b = df_3b[df_3b['batter'] == 660271]
#singleのデータを取り出す
df_1b = df[df['events'] == 'single']
df_1b = df_1b[df_1b['batter'] == 660271]
#hit_bypitchのデータを取り出す
df_hbp = df[df['events'] == 'hit_by_pitch']
df_hbp = df_hbp[df_hbp['batter'] == 660271]

#outイベントのデータを取り出す
df_out = df[~df['events'].isin(['home_run', 'double', 'triple', 'single', 'hit_by_pitch'])]
df_out = df_out[df_out['batter'] == 660271]

df_1b = df_1b.dropna(subset=['plate_x','plate_z'])
df_hr = df_hr.dropna(subset=['plate_x','plate_z'])
df_2b = df_2b.dropna(subset=['plate_x','plate_z'])
df_3b = df_3b.dropna(subset=['plate_x','plate_z'])
df_hbp = df_hbp.dropna(subset=['plate_x','plate_z'])
df_out = df_out.dropna(subset=['plate_x','plate_z'])


#create scatter plot using 'plate_x' and 'plate_z' columns
plt.scatter(df_hr['plate_x'], df_hr['plate_z'], color='r', label='home_run')
plt.scatter(df_2b['plate_x'], df_2b['plate_z'], color='g', label='double')
plt.scatter(df_3b['plate_x'], df_3b['plate_z'], color='b', label='triple')
plt.scatter(df_1b['plate_x'], df_1b['plate_z'], color='c', label='single')
plt.scatter(df_hbp['plate_x'], df_hbp['plate_z'], color='m', label='hit_by_pitch')

# 最背面へ
plt.scatter(df_out['plate_x'], df_out['plate_z'], color='gray', label='out', marker='.', linestyle='dotted', zorder=0)


# add labels for x and y axis
plt.xlabel('Plate X')
plt.ylabel('Plate Z')

# add legend
plt.legend()

# show the plot
plt.show()
</code></pre>

![](https://pbs.twimg.com/media/FmZ_7jAagAAKz9x?format=png&name=small)

-------

# ストライクゾーンチェック

<pre><code>import matplotlib.pyplot as plt

df_strike = df[df['description'] == 'called_strike'].dropna(subset=['plate_x','plate_z'])

# create scatter plot using 'plate_x' and 'plate_z' columns
plt.scatter(df_strike['plate_x'], df_strike['plate_z'], color='gray', label='all strikes')

# add labels for x and y axis
plt.xlabel('Left/Right')
plt.ylabel('Height')

# add title to the plot
plt.title('Strike Zone')

# add legend to the plot
plt.legend()

# display the plot
plt.show()

</code></pre>

![](https://pbs.twimg.com/media/FmaAUjCaUAApiYH?format=png&name=360x360)