Example of how to add a frame to a seaborn heatmap figure in python
Plot a figure with seaborn heatmap
Example of how to plot a figure with seaborn heatmap
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.array([[25.55535942, 1.99598017, 9.78107706],
[ 4.95758736, 39.68268716, 16.78109873],
[ 0.45401194, 0.10003128, 0.6921669 ]])
df = pd.DataFrame(data=data)
fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,
fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82})
res.invert_yaxis()
plt.title('Seaborn heatmap - with frame')
plt.savefig('seaborn_heatmap_with_frame_and_cell_border_01.png')
plt.show()
Add a frame to the figure
To add a frame a solution is to add the following lines:
for _, spine in res.spines.items():
spine.set_visible(True)
in
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.array([[25.55535942, 1.99598017, 9.78107706],
[ 4.95758736, 39.68268716, 16.78109873],
[ 0.45401194, 0.10003128, 0.6921669 ]])
df = pd.DataFrame(data=data)
fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,
fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82})
res.invert_yaxis()
# make frame visible
for _, spine in res.spines.items():
spine.set_visible(True)
plt.title('Seaborn heatmap - with frame')
plt.savefig('seaborn_heatmap_with_frame_01.png')
plt.show()
Add cell borders
Another option is to use the arguments linewidths=0.1 and linecolor='gray':
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = np.array([[25.55535942, 1.99598017, 9.78107706],
[ 4.95758736, 39.68268716, 16.78109873],
[ 0.45401194, 0.10003128, 0.6921669 ]])
df = pd.DataFrame(data=data)
fig = plt.figure(num=None, figsize=(10, 10), dpi=80, facecolor='w', edgecolor='k')
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
res = sns.heatmap(df, annot=True, vmin=0.0, vmax=100.0,
fmt='.2f', cmap=cmap, cbar_kws={"shrink": .82},
linewidths=0.1, linecolor='gray')
res.invert_yaxis()
plt.title('Seaborn heatmap - with frame')
plt.savefig('seaborn_heatmap_with_frame_and_cell_border_01.png')
plt.show()
References
Links | Site |
---|---|
seaborn heatmap with frames | stackoverflow |
seaborn.heatmap | seaborn doc |