Kenge
Kenge

Reputation: 1

outline one element of seaborn heatmap

Can I put an outline around one element in a seaborn heatmap?

e.g., I want to put a yellow outline around the top-left element in this figure:

import matplotlib.pyplot as plt
import seaborn as sb

matrix = [[1, 2], [3, 4]]
index_of_element_to_outline = [0, 0]

sb.heatmap(matrix)
# insert some code to outline the given element 
plt.show()

Upvotes: 0

Views: 392

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339320

Adding a rectangle is as easy as,

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

matrix = [[1, 2], [3, 4]]
index_of_element_to_outline = [0, 0]

ax = sns.heatmap(matrix)
rect = plt.Rectangle(index_of_element_to_outline, 1,1, color="gold", 
                     linewidth=3, fill=False, clip_on=False)
ax.add_patch(rect)

plt.show()

enter image description here

For a more sophisticated solution, where the rectangle does not overlay the pixel it encompasses, see Set matplotlib rectangle edge to outside of specified width?.

Upvotes: 1

Related Questions