如何创建 seaborn.heatmap() 并在图块周围添加边框?
How to create a seaborn.heatmap() with frames around the tiles?
我用 seaborn.heatmap() 渲染了一个热图效果很好。但是,出于某种目的,我需要围绕情节设置框架。
matplotlib.rcParams['axes.edgecolor'] = 'black'
matplotlib.rcParams['axes.linewidth'] = 1
两者都不行。
不知道是否有相应的技术命令,但如果您想模仿该行为,只需尝试使用 axhline
和 axvline
:
import string
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
letters = string.ascii_letters
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
columns=list(letters[:26]))
# Compute the correlation matrix
corr = d.corr()
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
ax = sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=5, yticklabels=5,
linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
ax.axhline(y=0, color='k',linewidth=10)
ax.axhline(y=corr.shape[1], color='k',linewidth=10)
ax.axvline(x=0, color='k',linewidth=10)
ax.axvline(x=corr.shape[0], color='k',linewidth=10)
plt.show()
,结果为:
ax = sns.heatmap(x)
for _, spine in ax.spines.items():
spine.set_visible(True)
sns.heatmap(数据集,线宽=1,线色='black')
我用 seaborn.heatmap() 渲染了一个热图效果很好。但是,出于某种目的,我需要围绕情节设置框架。
matplotlib.rcParams['axes.edgecolor'] = 'black'
matplotlib.rcParams['axes.linewidth'] = 1
两者都不行。
不知道是否有相应的技术命令,但如果您想模仿该行为,只需尝试使用 axhline
和 axvline
:
import string
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
letters = string.ascii_letters
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
columns=list(letters[:26]))
# Compute the correlation matrix
corr = d.corr()
# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
ax = sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
square=True, xticklabels=5, yticklabels=5,
linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
ax.axhline(y=0, color='k',linewidth=10)
ax.axhline(y=corr.shape[1], color='k',linewidth=10)
ax.axvline(x=0, color='k',linewidth=10)
ax.axvline(x=corr.shape[0], color='k',linewidth=10)
plt.show()
,结果为:
ax = sns.heatmap(x)
for _, spine in ax.spines.items():
spine.set_visible(True)
sns.heatmap(数据集,线宽=1,线色='black')