首先是热力图(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()
本文介绍了如何使用matplotlib库绘制热力图和折线图。在热力图的绘制中,作者建议直接使用matplotlib,因为修改更灵活,并分享了一个小技巧,即在matplotlib 3.0版本中可能出现边缘显示不完整的问题,而在2.0版本中能正确显示。此外,文章还涵盖了折线图的绘制方法。
&spm=1001.2101.3001.5002&articleId=105840838&d=1&t=3&u=141f28e81907415ba9278cb7cd9c53a9)
5万+

被折叠的 条评论
为什么被折叠?



