1. 参数详解

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

vmax,vmin, 图例中最大值和最小值的显示值,没有该参数时默认不显示

linewidths,热力图矩阵之间的间隔大小

cmap,热力图颜色

ax,绘制图的坐标轴,否则使用当前活动的坐标轴。

annot,annotate的缩写,annot默认为False,当annot为True时,在heatmap中每个方格写入数据。

annot_kws,当annot为True时,可设置各个参数,包括大小,颜色,加粗,斜体字等:

sns.heatmap(x, annot=True, ax=ax2, annot_kws={'size':9,'weight':'bold', 'color':'blue'})

fmt,格式设置,决定annot注释的数字格式,小数点后几位等;

cbar : 是否画一个颜色条

cbar_kws : 颜色条的参数,关键字同 fig.colorbar,可以参考:matplotlib自定义colorbar颜色条-以及matplotlib中的内置色条。

mask,遮罩

使用时有两个小技巧,

(1)先用sns.set(font_scale)修改字体比例:

sns.set(font_scale=1.5)

(2)再用plt.rc对全图字体进行统一修改:

plt.rc('font',family='Times New Roman',size=12)

2. 颜色效果

cmap的参数如下:

Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu(绿到蓝), GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd(橘色到红色), OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia(蓝绿黄), Wistia_r, YlGn,YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r,YlOrRd(红橙黄), YlOrRd_r, afmhot, afmhot_r,autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm(蓝到红), coolwarm_r, copper(铜色), copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r(红黄), hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r,rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer (黄到绿), summer_r(绿到黄), tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, twilight, twilight_r, twilight_shifted, twilight_shifted_r,viridis,viridis_r, vlag, vlag_r, winter, winter_r

示范如下:

cmap="YlGnBu":数字越大,颜色越深

cmap="YlGnBu_r":数字越大,颜色越浅

cmap="hot":黄色到红色,数字越大,颜色越浅

cmap="hot_r":红色到黄色,数字越大,颜色越深

cmap="OrRd":深红色到浅红色,类似“Oranges”。

cmap="autumn":黄色到红色

cmap="greens":绿色,数字越大,颜色越深

cmap="viridis":黄到蓝

cmap="greys":灰色

cmap="Purples":紫色

cmap="rainbow":彩虹色

cmap="gist_rainbow":彩虹色

将colormap置于特定值的中心(参考链接):

>>> ax = sns.heatmap(flights, center=flights.loc["January", 1955])

使用遮罩绘制矩阵中的一部分

>>> corr = np.corrcoef(np.random.randn(10, 200))

>>> mask = np.zeros_like(corr)

>>> mask[np.triu_indices_from(mask)] = True

>>> with sns.axes_style("white"):

... ax = sns.heatmap(corr, mask=mask, vmax=.3, square=True)

np.zeros_like() 返回一个零数组,其形状和类型与给定的数组相同;

np.triu_indices_from(mask) 返回数组上三角形的索引。

以下是一些网络上发现的配色好看的图:

原文链接: https://www.php.cn/python-tutorials-391565.html

# cmap用cubehelix map颜色

cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)

sns.heatmap(pt, linewidths = 0.05, ax = ax1, vmax=900, vmin=0, cmap=cmap)

# cmap用matplotlib colormap

sns.heatmap(pt, linewidths = 0.05, ax = ax2, vmax=900, vmin=0, cmap='rainbow')

#center的用法(颜色)

f, (ax1,ax2) = plt.subplots(figsize = (6, 4),nrows=2)

cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)

sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None )

#设置x轴图例为空值ax1.set_ylabel('kind')

# 当center设置小于数据的均值时,生成的图片颜色要向0值代表的颜色一段偏移

sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=200)

#robust的用法(颜色)

f, (ax1,ax2) = plt.subplots(figsize = (6,4),nrows=2)

cmap = sns.cubehelix_palette(start = 1.5, rot = 3, gamma=0.8, as_cmap = True)

sns.heatmap(pt, linewidths = 0.05, ax = ax1, cmap=cmap, center=None, robust=False )

sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=None, robust=True )

#mask对某些矩阵块的显示进行覆盖

p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, xticklabels=True, mask=(pt<800))

#mask对pt进行布尔型转化,结果为True的位置用白色覆盖

用mask实现:突出显示某些数据

sns.heatmap(x, mask=x < 1, ax=ax2, annot=True, annot_kws={"weight":"bold"})   #把小于1的区域覆盖掉

3. 个性化设置

个性化设置参考:Python - Seaborn可视化:图形个性化设置的几个小技巧

将x轴刻度放置在top位置的几种方法

# 将x轴刻度放置在top位置的几种方法

# ax.xaxis.set_ticks_position(‘top‘)

ax.xaxis.tick_top()

# ax.tick_params(axis=‘x‘,labelsize=6, colors=‘b‘, labeltop=True, labelbottom=False)

设置坐标轴刻度参数,”axis”不写的时候,默认是x轴和y轴的参数同时调整。

# 设置坐标轴刻度的字体大小

# matplotlib.axes.Axes.tick_params

ax.tick_params(axis=‘y‘,labelsize=8) # y轴

旋转轴刻度上文字方向的两种方法

# 旋转轴刻度上文字方向的两种方法

ax.set_xticklabels(ax.get_xticklabels(), rotation=-90)

# ax.set_xticklabels(corr.index, rotation=90)

更多请见:

设置Matplotlib颜色条大小以匹配图形(Set Matplotlib colorbar size to match graph)

seaborn基本使用

4. 代码备份

'''

深入挖掘

'''

font = {'family': 'Times New Roman',

'size': 12,

}

sns.set(font_scale=1.2)

plt.rc('font',family='Times New Roman')

fig = plt.figure(figsize = (16, 12))

ax1=fig.add_subplot(2,1,1)

cor = SubShowFeatures01.corr()

mask = np.zeros_like(cor)

for i in range(len(mask)):

for j in range(i+1, len(mask[0])):

mask[i][j] = True

sns.heatmap(cor,linewidths = 0.05, ax=ax1, mask=mask, annot=True, annot_kws=font, vmax=1.0, vmin=-1.0, cmap='YlGnBu', center=0.5,

cbar=True, robust=False)

ax1.set_title('User features - 01', fontdict=font)

ax2=fig.add_subplot(2,1,2)

cor = SubShowFeatures02.corr()

mask = np.zeros_like(cor)

for i in range(len(mask)):

for j in range(i+1, len(mask[0])):

mask[i][j] = True

sns.heatmap(cor,linewidths = 0.05, ax=ax2, mask=mask, annot=True, annot_kws=font, vmax=1.0, vmin=-1.0, cmap='YlOrRd', center=0)

ax2.set_title('User features - 02', fontdict=font)

plt.show()

python热力图颜色设置_【Python】绘制热力图seaborn.heatmap,cmap设置颜色的参数相关推荐

  1. 【画图】Python绘制热力图方法以及保存不全问题(seaborn.heatmap)

    0 前言 鉴于Matlab画图已经被封,自此画图战线全部转移到Python上来,这篇博客描述了Python画热力图的方法以及我踩到的坑. 1 程序 1.1 导入包 这里使用seaborn的heatma ...

  2. python输出文本居中_#python PIL ImageDraw text 文本居中#

    python pip pil有什么东西 你所问的问题实是属1.先参考[教程]Python中的内置的和方的模块搞懂PIL是属于第三方Python模块2.再参考:[待完善][总结]Python安装第三方的 ...

  3. python并行线程倒计时_[python 并行2]线程

    线程篇 基本使用 python线程使用的两个模块为: _thread (不推荐再使用). threading (查看threading的源码可以发现,threading实际是对_thread进一步的封 ...

  4. python查看excel编码格式_[Python]实现处理读写xlsx xls excel文件格式(含中文处理方法)...

    最近有个需求要处理excel 格式的数据,数据量比较大.用传统的语言似乎不太好处理,于是改用python实现,这里记录一下实现过程. 首先,科普一下xlsx xls的excel文件区别是什么. xls ...

  5. python shell如何打开_“python shell怎么打开“python shell启动教程

    python shell怎么打开 1.简介:如何在python中运行shell(bash命令) 2.工具/原料:python库:os.py 3.方法:import os command = 'date ...

  6. python多进程编程实例_[python] Python多进程编程技术实例分析

    这篇文章主要介绍了Python多进程编程技术,包括了线程.队列.同步等概念及相关的技巧总结,需要的朋友可以参考下 本文以实例形式分析了Python多进程编程技术,有助于进一步Python程序设计技巧. ...

  7. python快速爬虫视频_“python怎么快速爬虫视频“python 爬网页视频教程

    用python怎样爬网页 # coding:utf8 import cookielib import urllib2 url = "http://blog.uouo123.com" ...

  8. python朋友圈刷屏_“Python太火了!请救救Java!”9万程序员刷屏朋友圈 !

    没想到有生之年,笔者能观察到"霸主陨落"的过程,继PLPY4月榜单官宣,Python躺赢,再度"夺"冠,实力甩下Java和C后,近期,Stack Overflo ...

  9. python 打包 小文件_[Python][小知识][NO.5] 使用 Pyinstaller 打包成.exe文件

    1.安装 pyinstaller 插件 cmd命令:pip install PyInstaller PS . o.o 不知道 easy_install 的百度吧. 2.pyinstaller 简介 他 ...

  10. mumu模拟器cpu设置_网易MuMu模拟器CPU虚拟化怎么设置?

    网易MuMu模拟器CPU虚拟化怎么设置?网易MuMu模拟器CPU虚拟化如何开启?今天小编就为大家带来网易MuMu模拟器CPU虚拟化设置方法,一起来看看网易MuMu模拟器CPU虚拟化怎么设置吧! 网易M ...

最新文章

  1. PHP如何进阶,提升自己
  2. gulp将多张小图自动合成雪碧图
  3. AI芯片:从历史看未来
  4. Echarts4.0 使用系列——折线图,这里可能有你需要的
  5. 牛客网 2018年全国多校算法寒假训练营练习比赛(第三场)D.小牛vs小客-博弈
  6. Python学习并发与多线程
  7. Ubuntu和Windows默认系统启动顺序修改
  8. 假如 Go 能说话,听听 GMP 的心声
  9. spark官方文档_Spark整合Ray思路漫谈
  10. linux 下运行 jar包 java.lang.ClassNotFoundException: 解决办法
  11. 【辨异】—— 可见 vs. 不可见
  12. pytorch函数之nn.Linear
  13. 【Tensorflow踩过的坑儿】pb转pbtxt
  14. 使用ImageJ软件计算信噪比(SNR)的小工具
  15. html点击按钮展开文字,JS 展开/收起按钮显示隐藏文字示例
  16. 如何让芯烨打印机支持打印二维码
  17. 从事大数据行业5年,峰哥总结出了以下 7 条建议
  18. 如何使计算机用户具有管理权限,如何把电脑普通用户权限提升超级管理员账户...
  19. Unity3D教程笔记——unity初始02
  20. 什么是localhost(127.0.0.1)?

热门文章

  1. 使用idea创建一个jsp项目
  2. 中国互联网向收费模式转型 依靠虚拟商品营利
  3. 通讯协议与即时通讯杂谈
  4. 2022腾讯数字生态大会-腾讯云原生专场邀请函
  5. 根据 基因名、bed 文件的基因位置,提取 DNA 序列 bedtools
  6. Django Rest Framework - 实例PartyDemo 之 增删改查
  7. 从小到大度过最冷的一个冬天!
  8. 日常用语-从回家到就寝
  9. 最新研究发现:天然海绵含有抑制Omicron变体感染的天然化合物
  10. [08.03.14-15] 虐,并快乐着. 东西天目七尖穿越