python绘制热力图

本来想听着歌学习的结果发现没带,没关系不听歌也能专心学习!

如何绘制热力图

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# 正确显示中文
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
x_ticks=['是','否'] # 自定义xy轴
y_ticks=['是','否']
value=[[0.9769,0.0857],[0.0231,0.9143]]
sns.heatmap(value,xticklabels=x_ticks,yticklabels=y_ticks,annot=True,fmt='.4f',cmap='Blues',ax=ax[0])
ax[0].set_title("随机森林")
ax[0].set_xlabel("观测值")
ax[0].set_yticklabels(ax[0].get_yticklabels(), rotation=0) # 改变y轴标签的角度
ax[0].set_ylabel("预测值")

如何绘制子图

import matplotlib.pyplot as plt
fig,ax=plt.subplots(1,3,figsize=(10,4)) # 行数、列数、子图大小
plt.tight_layout(pad=2)  # 设置子图之间的间距

总和

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

plt,ax = plt.subplots(1,3,figsize=(10, 4))
plt.tight_layout(pad=2) 
x_ticks=['是','否']
y_ticks=['是','否']
value=[[0.9769,0.0857],[0.0231,0.9143]]
sns.heatmap(value,xticklabels=x_ticks,yticklabels=y_ticks,annot=True,fmt='.4f',cmap='Blues',ax=ax[0])
ax[0].set_title("随机森林")
ax[0].set_xlabel("观测值")
ax[0].set_yticklabels(ax[0].get_yticklabels(), rotation=0)
ax[0].set_ylabel("预测值")
x_ticks=['是','否']
y_ticks=['是','否']
value=[[0.975,0.1091],[0.025,0.8909]]
sns.heatmap(value,xticklabels=x_ticks,yticklabels=y_ticks,annot=True,fmt='.4f',cmap='Blues',ax=ax[1])
ax[1].set_title("XGboost")
ax[1].set_xlabel("观测值")
ax[1].set_yticklabels(ax[0].get_yticklabels(), rotation=0)
ax[1].set_ylabel("预测值")
x_ticks=['是','否']
y_ticks=['是','否']
value=[[0.9547,0.2987],[0.0453,0.7013]]
sns.heatmap(value,xticklabels=x_ticks,yticklabels=y_ticks,annot=True,fmt='.4f',cmap='Blues',ax=ax[2])
ax[2].set_title("BP")
ax[2].set_xlabel("观测值")
ax[2].set_yticklabels(ax[0].get_yticklabels(), rotation=0)
ax[2].set_ylabel("预测值")
plt.savefig('C:Desktop/tree.png',dpi=150)

你可能感兴趣的:(python,开发语言)