HeatMap Practice¶

This is a project to practice creating a heatmap, using a table I quickly made with my income/expenses for each week of a year.¶

In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

This was a practice project, importing a table I quickly made with with income/expense data for every week in a year.¶

In [2]:
df = pd.read_csv('MoneyWeeks_ForHeatMap.csv')
In [3]:
df.head(10)
Out[3]:
Week Transaction
0 1 1106.52
1 1 -346.17
2 2 1076.56
3 2 -338.12
4 3 894.29
5 3 -423.59
6 4 1050.88
7 4 -208.13
8 5 1014.59
9 5 -478.43

Grouping the data into Total Profit each week¶

In [4]:
grouped_df = df.groupby('Week').agg({'Transaction': 'sum'})
result_df = grouped_df.reset_index()
result_df = result_df.rename(columns={'Transaction': 'Total Profit'})
In [5]:
result_df.head()
Out[5]:
Week Total Profit
0 1 760.35
1 2 738.44
2 3 470.70
3 4 842.75
4 5 536.16

To create a heatmap this data needed turning into a pivot table¶

In [6]:
df_pivot = result_df.pivot_table(values='Total Profit', index=result_df.index // 4, columns=result_df.index % 4)

Creating the pivot table¶

In [7]:
plt.figure(figsize=(12, 8))
heatmap = sns.heatmap(df_pivot, cmap='YlGnBu', annot=True,  fmt=".2f",linewidths=.5, cbar_kws={'label': 'Profit (£)'})

heatmap.set_yticklabels([f"Period {i}" for i in range(1, 14)], rotation=0)
heatmap.set_xticklabels([f"Week {i}" for i in range(1, 5)], rotation=45)

plt.title('Weekly Profit Heatmap')
plt.show()
In [ ]:
 
In [ ]:
 
In [ ]: