Skip to content Skip to sidebar Skip to footer

Heatmap-like Plot, But For Categorical Variables In Seaborn

Same question as heatmap-like plot, but for categorical variables but using python and seaborn instead of R: Imagine I have the following dataframe: df = pd.DataFrame({'John':'No Y

Solution 1:

You can use a discrete colormap and modify the colorbar, instead of using a legend.

value_to_int = {j:i for i,j in enumerate(pd.unique(df.values.ravel()))} # like you did
n = len(value_to_int)     
# discrete colormap (n samples from a given cmap)
cmap = sns.color_palette("Pastel2", n) 
ax = sns.heatmap(df.replace(value_to_int), cmap=cmap) 
# 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.show()

categorical seaborn heatmap

The colorbar part is adapted from this answer

HTH

Solution 2:

I would probably use bokeh for this purpose as it has categorical heatmaps built in. Y-axis labels are written horizontally too, which is more readable.

http://docs.bokeh.org/en/0.11.1/docs/gallery/heatmap_chart.html

Post a Comment for "Heatmap-like Plot, But For Categorical Variables In Seaborn"