python draw matrix weights

Reference: 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() parameters

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: Matrix data set, can be a numpy array (array), if it is a pandas dataframe, the index/column information of df will correspond to the columns and rows of the heatmap respectively
  • linewidths, the size of the interval between the heat map matrices
  • vmax,vmin, the display value of the maximum and minimum values ​​in the legend, it is not displayed by default without this parameter
  • cmap: The colormap name or color object of matplotlib; if not provided, the default is cubehelix map (when the data set is a continuous data set) or RdBu_r (when the data set is a discrete data set)
  • center: Set the data to the mean data in the legend, that is, the data value in the center of the legend; by setting the center value, you can adjust the overall depth of the generated image color; when setting the center data, if there is data overflow, manually set vmax, vmin will automatically change
  • xticklabels: If it is True, draw the column names of the dataframe. If it is False, the column names are not drawn. If it is a list, the content in the list is drawn as xticklabels. If it is an integer n, the column names are drawn, but a label is drawn for each n. The default is True.
  • yticklabels: If True, draw the row names of the dataframe. If it is False, the row name is not drawn. If it is a list, the content in the list is drawn as yticklabels. If it is an integer n, the column names are drawn, but a label is drawn for each n. The default is True. The default is True.
  • anot: the abbreviation of annotate, annot defaults to False, when annot is True, write data to each square in the heatmap

 

Guess you like

Origin blog.csdn.net/Answer3664/article/details/106746386