import numpy as np
import seaborn as sns
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
values = np.random.rand(3, 3)
x_ticks = ['x-1', 'x-2', 'x-3']
y_ticks = ['y-1', 'y-2', 'y-3']
ax = sns.heatmap(values, xticklabels=x_ticks, yticklabels=y_ticks)
ax.set_title('Heatmap for test')
ax.set_xlabel('x label')
ax.set_ylabel('y label')
plt.show()
figure = ax.get_figure()
figure.savefig('sns_heatmap.jpg')
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, annot=True, fmt="d") # 将每个方格的数据显示出来
import numpy as np
import seaborn as sns
values = np.random.rand(5, 5)
ax = sns.heatmap(values, cmap="YlGnBu", annot=True, linewidths=.5) # 修改颜色,添加线宽
values = np.random.rand(5, 5)
ax = sns.heatmap(values, cmap="YlGnBu", xticklabels=2, yticklabels=False) # x轴变2倍,隐藏y轴
# 使用掩码,绘制部分数据
corr = np.corrcoef(np.random.randn(10, 200))
mask = np.zeros_like(corr)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)
data = np.random.rand(10, 12)
f, ax = plt.subplots(figsize=(8,5))
ax = sns.heatmap(data,cmap = 'RdBu',ax=ax,vmin=0, vmax=1,annot=True,fmt ='0.1g')
#设置坐标字体方向
ax.set_yticklabels(ax.get_yticklabels(), rotation=45)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
plt.show()
参考链接