Home > Mobile >  Heatmap of average engagement across weekdays and hour if the day
Heatmap of average engagement across weekdays and hour if the day

Time:03-04

I have a data frame like this

Engagement   ...    Weekday    Hour
13000        ...    3          12
25000        ...    4          19
9000         ...    0          23
...          ...    ...        ...

I would like to plot with seaborn (or matplot) a heatmap that shows the average engagement per weekday and hour, with the weekday on the x-axis, the hour on the y-axis and the intensity of the colour for the engagement value. It should come out something like this enter image description here

CodePudding user response:

Try:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

rng = np.random.default_rng(2022)
df = pd.DataFrame({'Engagement': rng.integers(1000, 100000, 1000),
                   'Weekday': rng.integers(0, 7, 1000),
                   'Hour': rng.integers(0, 24, 1000)})

out = df.groupby(['Hour', 'Weekday'])['Engagement'].mean().unstack()
sns.heatmap(out)
plt.show()

enter image description here

  • Related