pythonseabornheatmapcolorbarcolor-scheme

Add edges to colorbar in seaborn heatmap


I have the following heatmap:

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

# Create a sample heatmap
data = np.random.rand(10, 12)
ax = sns.heatmap(data)


plt.show()

How can I add a black edge to the colormap? Is there any way to do this without needing to use subplots?


Solution

  • Looks like seaborn turns the edges off by default.

    Here's an approach to get a reference to the colorbar Axes, and then re-apply the edge:

    ax = sns.heatmap(data)
    
    cax = ax.figure.get_children()[-1]
    cax.spines['outline'].set_linewidth(0.5) # adjust as desired
    

    Output:

    enter image description here