Skip to content Skip to sidebar Skip to footer

Seaborn Heat Map For Week Vs Day Python

I need to generate a heat map Where I have to arrange days as columns and week_num as rows and Green for a positive day and red for the negative day. It should have break for each

Solution 1:

I am guessing you refer to the day of the week, otherwise it will be a really weird heatmap. You can try something like below, basically in something like your data.frame, get the day of week as another column, then pivot this into a wide format and plot. sns.heatmap does not take in categorical values so you need to replace this with 0,1 and label them accordingly in the legend:

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

dates = pd.date_range(start='1/1/2018', periods=60, freq='1D')
color_code = np.random.choice(['green','red'],60)
df = pd.DataFrame({'dates':dates ,'color_code':color_code})
df['week_num'] = df['dates'].dt.strftime("%W")
df['day_num'] = df['dates'].dt.weekday

fig, ax = plt.subplots(1, 1, figsize = (5, 3))

df_wide = df.pivot_table(index='week_num',columns='day_num',values='color_code',
aggfunc=lambda x:x)
sns.heatmap(df_wide.replace({'green':0,'red':1}),cmap=["#2ecc71","#e74c3c"],
            linewidths=1.0,ax=ax)
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.25,0.75])
colorbar.set_ticklabels(['green','red'])

enter image description here


Post a Comment for "Seaborn Heat Map For Week Vs Day Python"