效果图:
主要步骤:
1. 数据准备:随机赋值
2. 图像绘制:绘制热力图
详细代码:着急的直接拖到最后有完整代码
步骤一:导入库包及图片存储路径并设置中文字体为宋体,西文为新罗马(没有的库包要先下好奥)
###############################################################################
# 导入库及文件
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sns
config = {
"font.family": 'serif',
"font.size": 12,
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
rcParams['axes.unicode_minus']=False
步骤二:生成数据
###############################################################################
figpath = r'H:/00.csdn/02fig/'
# 生成数据
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
步骤三:绘制图像
# 绘制图像
fig, ax = plt.subplots()
x_labels = ["a", "b", "c", "d", "e", "f", "g"]
y_labels = ["A", "B", "C", "D", "E", "F", "G"]
plot = sns.heatmap(data, cmap='YlGn', annot=True, xticklabels=x_labels,yticklabels=y_labels)
步骤四:保存图像
###############################################################################
# 输出并保存图像
plt.savefig(figpath+'008 热力图.png', bbox_inches = 'tight', dpi=600, format='png')
plt.show()
完整代码在这里:
###############################################################################
# 导入库及文件
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sns
config = {
"font.family": 'serif',
"font.size": 12,
"mathtext.fontset": 'stix',
"font.serif": ['SimSun'],
}
rcParams.update(config)
rcParams['axes.unicode_minus']=False
###############################################################################
figpath = r'H:/00.csdn/02fig/'
# 生成数据
data = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],
[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
# 绘制图像
fig, ax = plt.subplots()
x_labels = ["a", "b", "c", "d", "e", "f", "g"]
y_labels = ["A", "B", "C", "D", "E", "F", "G"]
plot = sns.heatmap(data, cmap='YlGn', annot=True, xticklabels=x_labels,yticklabels=y_labels)
###############################################################################
# 输出并保存图像
plt.savefig(figpath+'008 热力图.png', bbox_inches = 'tight', dpi=600, format='png')
plt.show()