【基础绘图】08.热力图

效果图:

主要步骤:

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()
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值