[PYTHON] Visualize B League goals and goals on a heat map

Purpose

Create a heat map of goals and goals from B League match results

heatmap

Implementation

Data acquisition

Get B League match results from this site https://www.bleague.jp/schedule/?s=1&tab=1&year=2019&event=2&club=&setuFrom=1

Get using Python's Selenium Get the results in pandas.DataFrame

result

データ取得DF

Plastic surgery

Labeling score distribution

Since basketball has a wider range of points than soccer and baseball, we will create a distribution table separated by 10 points.

Add a score column by referring to this article https://qiita.com/kshigeru/items/bfa8c11d1e6487c791d3

python


labels = [ "{0} - {1}".format(i, i + 9) for i in range(40, 200, 10) ]
df['Home score distribution'] = pd.cut(df['Home score'], np.arange(40, 201, 10), labels=labels)
df['Away score distribution'] = pd.cut(df['Away score'], np.arange(40, 201, 10), labels=labels)

Label 40 to 200 points with 10 points. NaN is set for those outside the range

result

得点頒布のラベル付けDF

Grouping by home / away

Since it can be expected that there will be a difference between the home record and the away record, Create home battle heatmaps, away battle heatmaps, and all battle heatmaps

python


for team in teamList:
    teamHomeDf = df.query('home== @team')
    teamAweyDf = df.query('Away== @team')
    teamDf = pd.concat([teamHomeDf, teamAweyDf])

The teamList contains the team names of all teams

result

グルーピングDF

Data creation for heatmap

Format the data as shown to create a heatmap ヒートマップ用のデータ作成DF

python


distributionDf = teamHomeDf.groupby(['Home score distribution', 'Away score distribution'])
distributionDf = distributionDf.size().unstack()
distributionDf = distributionDf.fillna(0)

cols = distributionDf.columns
indexs = distributionDf.index

for index in indexs:
	for col in cols:
		allDf.at[index, col] = round(distributionDf.at[index, col] / total, 2)

plt.figure(figsize=(8, 8))
sns.heatmap(allDf, annot = True, cmap = color, vmin = 0, square = True)
if not os.path.exists(path):
	os.makedirs(path)
plt.savefig(path + '/' + fileNm)
plt.close('all')

Set the data as shown in the figure in allDf. allDf

output

Outputs a heat map based on the formatted data Click here for heat map https://note.nkmk.me/python-seaborn-heatmap/ https://matplotlib.org/tutorials/colors/colormaps.html

python


plt.figure(figsize=(8, 8))
sns.heatmap(allDf, annot = True, cmap = 'hot', vmin = 0, square = True)
if not os.path.exists(path):
	os.makedirs(path)
plt.savefig(path + '/' + fileNm)
plt.close('all')

Output heatmap

allDf

Recommended Posts

Visualize B League goals and goals on a heat map
Visualize and understand Japan's regional mesh on a map
Folium: Visualize data on a map with Python
Visualize grib2 on a map with python (matplotlib)
Python a + = b and a = a + b are different
[Python] Visualize overseas Japanese soccer players on a map as of 2021.1.1
Visualize prefectures with many routes by prefecture on a Japanese map
[Python] return A [or / and] B
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Create a web map using Python and GDAL
python memo-"if not A and B" was "if (not A) and B"
[Rails] How to display multiple markers on Google Map and display a balloon when clicked