I would like to only include certain values in a seaborn heatmap legend. Specifically, I have a "nan" category that I don't want to see in the legend.
I am trying to plot ward movements for patients in a hospital as a kind of categorical heatmap, with different colours representing different wards. I have borrowed from this code heatmap-like plot, but for categorical variables in seaborn to configure my input table for the heatmap. Blank cells mean the patient was not in the hospital on those dates.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import pandas as pd
data = {'12/3': [np.nan, 'Ward_B', np.nan],
'13/3': [np.nan, 'Ward_B', np.nan],
'14/3': [np.nan, 'Ward_B', 'ED'],
'15/3': ['ED', 'Ward_A', 'Ward_C'],
'16/3': ['ED', 'Ward_A', 'Ward_C'],
'17/3': ['Ward_A', 'Ward_A', 'Ward_C'],
'18/3': ['Ward_A', np.nan, 'Ward_C'],
'19/3': ['Ward_A', np.nan, 'Ward_A'],
'20/3': [np.nan, np.nan, 'Ward_A']}
df = pd.DataFrame (data, columns = ['12/3',
'13/3',
'14/3',
'15/3',
'16/3',
'17/3',
'18/3',
'19/3',
'20/3'])
# Create dataframe of patient IDs
patient_codes_df = pd.DataFrame(['Patient_A', 'Patient_B', 'Patient_C'])
# change heading
patient_codes_df = patient_codes_df.rename(columns={0:'Patient'})
# Merge
df2 = pd.concat([patient_codes_df, df], axis=1)
# Make Patient column the index
df3 = df2.set_index('Patient')
df3
df3 is what my input data looks like.
And this is how I'm plotting the heatmap
value_to_int = {j:i for i,j in enumerate(pd.unique(df3.values.ravel()))}
n = len(value_to_int)
cmap = sns.color_palette("Accent", n) # set colours
fig, ax = plt.subplots(1, 1, figsize = (6, 2), dpi=300)
mask = df3.isnull()
ax = sns.heatmap(df3.replace(value_to_int), cmap=cmap, mask=mask, linewidths=0.1, linecolor='#b5b5b5')
ax.set_ylabel('')
# modify colorbar:
colorbar = ax.collections[0].colorbar
r = colorbar.vmax - colorbar.vmin
colorbar.set_ticks([colorbar.vmin + r / n * (0.5 + i) for i in range(n)])
colorbar.set_ticklabels(list(value_to_int.keys()))
plt.xticks(rotation=90)
plt.show()
I would like to get rid of "nan" from the legend, and also re order so it goes in a sensible order like ED, Ward_A, Ward_B, Ward_C.
Thanks for your help.