matplotlib画图(折线图、热力图)

本文介绍了如何使用matplotlib库绘制热力图和折线图。在热力图的绘制中,作者建议直接使用matplotlib,因为修改更灵活,并分享了一个小技巧,即在matplotlib 3.0版本中可能出现边缘显示不完整的问题,而在2.0版本中能正确显示。此外,文章还涵盖了折线图的绘制方法。
Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

首先是热力图(heatmap),直接用matplotlib画就行了,不需要使用seaborn,seaborn应该就是用的matplotlib的代码,要修改东西还不如直接用matplotlib方便,另外本人在画热力图图时遇到,用matplotlib3.0出现画出的图会边缘的地方只显示半格,改成matplotlib2.0就正确了。

'''
以上省略数据处理,注意数据的shape必须是n*n
'''

fig, ax = plt.subplots()
im = ax.imshow(L2)   #imshow即画热力图
cbar=ax.figure.colorbar(im, ax=ax)  #设置旁边显示的bar

# We want to show all ticks...
Pruning_step = [9, 8, 7, 6, 5, 4, 3, 2, 1]  #坐标刻度值
gamma = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
ax.set_xticks(np.arange(9))
ax.set_yticks(np.arange(9))
# ... and label them with the respective list entries
ax.set_yticklabels(Pruning_step)  #设置坐标刻度值
ax.set_xticklabels(gamma)

valfmt=matplotlib.ticker.StrMethodFormatter('{x:.3f}')  #给热力图标注文本设置格式
# Loop over data dimensions and create text annotations.
for i in range(9):
    for j in range(9):
        text = ax.text(j, i, valfmt(L2[i, j], None),
                       ha="center", va="center", color="w", fontsize=10)

ax.set_xlabel(r"$\gamma$")
ax.set_ylabel("pruning step", fontdict={'family' : 'Times New Roman'})
# ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
plt.savefig('heatmap.pdf')
plt.show()



 然后是折线图:

'''
以上主要处理数据用的,注意输出的值必须是n*n
'''

fig, ax = plt.subplots()
im = ax.imshow(L2)   #imshow即画热力图
cbar=ax.figure.colorbar(im, ax=ax)  #设置旁边显示的bar

# We want to show all ticks...
Pruning_step = [9, 8, 7, 6, 5, 4, 3, 2, 1]  #坐标刻度值
gamma = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
ax.set_xticks(np.arange(9))
ax.set_yticks(np.arange(9))
# ... and label them with the respective list entries
ax.set_yticklabels(Pruning_step)  #设置坐标刻度值
ax.set_xticklabels(gamma)

valfmt=matplotlib.ticker.StrMethodFormatter('{x:.3f}')  #给热力图标注文本设置格式
# Loop over data dimensions and create text annotations.
for i in range(9):
    for j in range(9):
        text = ax.text(j, i, valfmt(L2[i, j], None),
                       ha="center", va="center", color="w", fontsize=10)

ax.set_xlabel(r"$\gamma$")
ax.set_ylabel("pruning step", fontdict={'family' : 'Times New Roman'})
# ax.set_title("Harvest of local farmers (in tons/year)")
fig.tight_layout()
plt.savefig('heatmap.pdf')
plt.show()



 

您可能感兴趣的与本文相关的镜像

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen3-32B-Chat 私有部署镜像 | RTX4090D 24G 显存 CUDA12.4 优化版

Qwen
文本生成
Qwen3

本镜像基于 RTX 4090D 24GB 显存 + CUDA 12.4 + 驱动 550.90.07 深度优化,内置完整运行环境与 Qwen3-32B 模型依赖,开箱即用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值