数据使用的是seborn内置数据
官网链接:https://seaborn.pydata.org/generated/seaborn.heatmap.html
data
import seaborn as sns
import matplotlib.pyplot as plt
glue = sns.load_dataset('glue').pivot("Model","Task","Score")
sns.heatmap(glue)
![data](https://img-blog.csdnimg.cn/b95b45325dfd41048a892ff67d68ba9a.png)
cmap
将数据值映射到颜色空间的不同颜色
cmap的可选值见另一篇博客:
https://blog.csdn.net/qq_43201025/article/details/131043840
sns.heatmap(glue,cmap='Reds')
![cmap](https://img-blog.csdnimg.cn/1cbc05eaea1748ad826b9df8a71e1b28.png)
linewidths、linecolor
- linewidths:线条宽度
- linecolor:线条颜色
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white')
![线条设置](https://img-blog.csdnimg.cn/60cdc4bcd4554c9582faec3231cd69d2.png)
square
如果为True,则将坐标轴的两个轴设置为长短相同,也就相当于每个单元格都是方形的
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True)
![square](https://img-blog.csdnimg.cn/c150d03a4bcf4e288abb629b5273d91d.png)
ax
指定绘制的轴,一般在plt.subplots需要在一个画板画多个子图时使用
f,ax = plt.subplots(1,1)
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,ax=ax)
![ax](https://img-blog.csdnimg.cn/ba9581770a554a8d8dedd1233cfbf061.png)
annot
如果为True,将数据值写入每个单元格。如果是与数据形状相同的数组,则将annot数组中的值写入热力图而不再是数据。
指定为True
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=True)
![annot](https://img-blog.csdnimg.cn/eb447d69b5764d4bb154f342c08b2b3d.png)
指定为同形状数组
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=np.random.randint(10,size=(8,8)))
![](https://img-blog.csdnimg.cn/c8050c30f90d4776923375d8092aa50a.png)
vmax、vmin
颜色映射的最大值和最小值,如果不指定,默认计算数据的最大值和最小值
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=True
,vmax=glue.max().max()
,vmin=glue.min().min())
![数据的最大最小值](https://img-blog.csdnimg.cn/074108faabe64808b5dfe3cbc76a8a82.png)
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=True
,vmax=100
,vmin=10)
![在这里插入图片描述](https://img-blog.csdnimg.cn/91e2ec3c278b4d90a01d0eee7f33fc2d.png)
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=True
,vmax=80
,vmin=30)
![在这里插入图片描述](https://img-blog.csdnimg.cn/c1b985a5e05f49e6a803f72fc7252e04.png)
annot_kws
设置数字展示的参数,例如大小、颜色等
常用的有:
- fontfamily或family「字体类型」: {FONTNAME, ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, ‘monospace’}
- fontsize或size「字体大小」: float or {‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’}
- fontweight或weight「粗细」: {a numeric value in range 0-1000, ‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}
- color「字体颜色」
- fontstyle或style「字体风格」: {‘normal’, ‘italic’, ‘oblique’}
具体设置见https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text
sns.heatmap(glue
,cmap='Reds'
,linewidths=0.1
,linecolor='white'
,square=True
,annot=True
,annot_kws={
"size": 12
, "family": "serif"
, "weight": "light"})
![设置字体格式](https://img-blog.csdnimg.cn/6cb7845f6f154ab9a9192a7913bfea48.png)
mask
如果使用该参数,数据将不会显示在mask为True的单元格中。缺少值的单元格将被自动屏蔽。
mask = (np.random.randint(2,size=(8,8))==0)
mask
![在这里插入图片描述](https://img-blog.csdnimg.cn/69c7d716ed2c43a3b090f185d3132afb.png)
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, mask=mask)
![mask](https://img-blog.csdnimg.cn/0ed4c056deee4ba1a5c8f402e5f903b7.png)
xticklabels、yticklabels
如果为True,默认使用数据的列名。如果为False,则不绘制列名。如果类似于列表,则将这些替代标签绘制为xticklabels。如果指定的是整数,则使用列名绘制,但每个n标签绘制一个。如果是“auto”,尝试密集地绘制不重叠的标签。
设置为auto
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, xticklabels='auto'
, yticklabels=False
)
![auto](https://img-blog.csdnimg.cn/c6686726f0364686b54e84de48fddfbd.png)
指定整数
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, xticklabels=4
, yticklabels=False
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/8ad842b178264569b7af44e035d998a5.png)
指定为True
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, xticklabels=True
, yticklabels=False
)
![True](https://img-blog.csdnimg.cn/31c14ceb0f144c8282caa529019901ae.png)
center
在绘制发散数据时使颜色图居中的值。如果没有指定,使用此参数将改变默认的cmap,还是会使用cmap的颜色,但是会改变对最大值到最小值之间颜色的渐变范围
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, center=0
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/cb82601408fe4e258e716cef5d197507.png)
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, center=100
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/380e328931cf498aa750c641f9356b00.png)
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, center=50
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/4845887845124b2588938836e046c5c3.png)
指定center前热力图为:
![在这里插入图片描述](https://img-blog.csdnimg.cn/4564d5fa46fc426da288bd597aef2992.png)
指定center前cmap='Reds’为
![](https://img-blog.csdnimg.cn/a3cf461625ec44abb0114648c0448c08.png)
robust
如果为True,并且没有设置vmin、vmax则使用robust分位数来映射数据的颜色
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, robust=True
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/4f16f57582f34e1bb70c73e085e4458e.png)
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, robust=True
, vmax= 90
, vmin=30
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/ee0ffcd366cf40ef9ec17bdf1c49832f.png)
fmt
添加注释时使用的字符串格式化代码。
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, fmt='.2f'
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/28d3f6d739b24796a29e40dbc5578a30.png)
cbar
是否绘制颜色条
sns.heatmap(glue
, cmap='Reds'
, linewidths=0.1
, linecolor='white'
, square=True
, annot= True
, cbar= False
)
![在这里插入图片描述](https://img-blog.csdnimg.cn/07feec1bf51a4c41bf2fde07aedd1710.png)