the man
the man

Reputation: 1391

How to create a frequency table of two columns and plot a heatmap

Does anyone know how a figure like below is constructed? Possibly using seaborn heatmap or a matplotlib tool?

I have the relevant data in a pandas dataframe; home goals & away goals for each match for the past 5 years for various football leagues, I'm just not sure how a figure like this would be constructed?

The data I have is in the following form;

FTHG  FTAG

 2      0
 3      1
 2      2
 1      2

FTHG = Full Time Home Goals FTAG = Full Time Away Goals

Football results description

Upvotes: 3

Views: 3120

Answers (1)

chitown88
chitown88

Reputation: 28620

Use the 2 columns to create crosstab (the matrix of the counts). Divide it by the total of matches to get the percent. Then put into heatmap.

You can play around with the sytles and such on your own.

import pandas as pd
import seaborn as sns

df = pd.DataFrame([[1,0],[2,1],[3,2],[4,2],[5,0],[6,1],[7,1],[2,5],[5,3],
                   [4,0],[1,1],[2,2],[4,3],[1,4],[2,5],[4,6],[1,7],[2,0],
                   [2,2],[0,0],[2,1],[1,2],[1,0],[2,0],[1,1],[1,1],[1,0],
                   [2,5],[0,0],[1,1],[1,3],[1,3],[2,2],[1,1],[0,1],[1,0]], 
                    columns=['FTHG','FTAG'])


df2 = pd.crosstab(df['FTHG'], df['FTAG']).div(len(df))
sns.heatmap(df2, annot=True)

Output:

enter image description here

Upvotes: 5

Related Questions