python 绘制矩阵权重

参考:https://blog.csdn.net/henbile/article/details/80241597

https://blog.csdn.net/m0_38103546/article/details/79935671

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
a = np.random.rand(4,3)
fig, ax = plt.subplots(figsize=(9,9))
#二维的数组的热力图,横轴和数轴的ticklabels要加上去的话,既可以通过将array转换成有column
#和index的DataFrame直接绘图生成,也可以后续再加上去。后面加上去的话,更灵活,包括可设置labels大小方向等。
sns.heatmap(pd.DataFrame(np.round(a,2), columns=['a', 'b', 'c'], index=range(1,5)),
                annot=True, vmax=1,vmin=0, xticklabels=True, yticklabels=True, square=True, cmap="YlGnBu")
#sns.heatmap(np.round(a,2), annot=True, vmax=1,vmin = 0, xticklabels= True, yticklabels= True,
#            square=True, cmap="YlGnBu")
ax.set_title('dsf', fontsize=18)
ax.set_ylabel('df', fontsize=18)
ax.set_xlabel('er', fontsize=18)

plt.show()

seaborn.heatmap()各个参数

seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annotkws=None, linewidths=0, linecolor='white', cbar=True, cbarkws=None, cbar_ax=None, square=False, ax=None, xticklabels=True, yticklabels=True, mask=None, **kwargs)

  • data:矩阵数据集,可以使numpy的数组(array),如果是pandas的dataframe,则df的index/column信息会分别对应到heatmap的columns和rows
  • linewidths,热力图矩阵之间的间隔大小
  • vmax,vmin, 图例中最大值和最小值的显示值,没有该参数时默认不显示
  • cmap:matplotlib的colormap名称或颜色对象;如果没有提供,默认为cubehelix map (数据集为连续数据集时) 或 RdBu_r (数据集为离散数据集时)
  • center:将数据设置为图例中的均值数据,即图例中心的数据值;通过设置center值,可以调整生成的图像颜色的整体深浅;设置center数据时,如果有数据溢出,则手动设置的vmax、vmin会自动改变
  • xticklabels: 如果是True,则绘制dataframe的列名。如果是False,则不绘制列名。如果是列表,则绘制列表中的内容作为xticklabels。 如果是整数n,则绘制列名,但每个n绘制一个label。 默认为True。
  • yticklabels: 如果是True,则绘制dataframe的行名。如果是False,则不绘制行名。如果是列表,则绘制列表中的内容作为yticklabels。 如果是整数n,则绘制列名,但每个n绘制一个label。 默认为True。默认为True。
  • anot: annotate的缩写,annot默认为False,当annot为True时,在heatmap中每个方格写入数据

猜你喜欢

转载自blog.csdn.net/Answer3664/article/details/106746386