# Purpose

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

# 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

## 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.

#### `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

### 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

### Data creation for heatmap

Format the data as shown to create a heatmap

#### `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.

## 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')
``````