RedandWhiteDays

赤、白、ときどき黒猫

DAY3 : matplotlibの背景画像をPillowを用いて設定する

まずは前回得られたshotsデータをmatplotlibでシンプルに可視化してみよう。
今回は元サイトとの差別化のためshotの起点に時間を記載してみた。

import matplotlib.pyplot as plt

fig=plt.figure()
ax=fig.add_subplot(111)
for i in range(len(shots)):
	if shots[i][5]=='blue':
		color='b'
	elif shots[i][5]=='red':
		color='r'
	elif shots[i][5]=='yellow':
		color='y'
	elif shots[i][5]=='darkgrey':
		color='k'
	ax.plot([float(shots[i][1]),float(shots[i][3])],[float(shots[i][2]),float(shots[i][4])],color)
        ax.text(float(shots[i][1]),float(shots[i][2]),shots[i][0])
ax.set_xlim(0,740)
ax.set_ylim(0,529)
ax.invert_yaxis()
plt.shot()


f:id:redandwhite:20160115162107p:plain

ピッチが背景に描写されていないので分かりにくい。
matplotlibでも背景画像を設定することができるようなのでやってみることにした。
tokeigaku.blog.jp


画像の読み込み自体はOpenCVかPillowという外部ライブラリを使用するよう。
今回はPillow(PIL)をpipでインストールして使ってみることにした。

import numpy as np
from PIL import Image

if pitch:
		im=Image.open("pitch_shot.png")
		im=np.array(im)
		ax.imshow(im)


このコードを上に追加して、pitch=Trueで実行することで得られた画像がこちら。
f:id:redandwhite:20160115162122p:plain

ロングシュートの起点がはみだしているもののなかなか良い感じ。