Home  >  Article  >  Backend Development  >  How to implement visual heat map in python

How to implement visual heat map in python

零到壹度
零到壹度Original
2018-04-04 14:22:2650240browse


This article mainly introduces how to implement visual heat map in python. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look

Heat map

1. Use heat map to see the similarity of multiple features in the data table. Refer to the official API parameters and address:

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

(1) Heat map input data parameters:

data: Matrix data set, yes It is an array of numpy, or it can be a DataFrame of pandas. If it is a DataFrame, the index/column information of df will correspond to the columns and rows of the heatmap respectively, that is, pt.index is the row label of the heat map, and pt.columns is the column label of the heat map

(2) Heat map matrix block color parameters:

vmax, vmin: are the maximum and minimum color value ranges of the heat map respectively. The default is determined based on the values ​​in the data table
cmap: Mapping from numbers to color space, the value is the colormap name or color object in the matplotlib package, or a list representing colors; change the parameter default value: set according to the center parameter
center: When the data table values ​​are different, set the color center alignment value of the heat map; 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 The set vmax and vmin will automatically change
robust: The default value is False; if it is False and the values ​​​​of vmin and vmax are not set, the color mapping range of the heat map is based on the robustness Quantile setting, instead of extreme value setting

(3) Heat map matrix block annotation parameters:

annot (abbreviation of annotate): default Value False; if it is True, data is written in each square of the heat map; if it is a matrix, data corresponding to the matrix is ​​written in each square of the heat map
fmt: String format Code, data format for identifying numbers on the matrix, such as retaining several digits after the decimal point
annot_kws: Default value is False; if it is True, set the size, color, and font of the numbers on the heat map matrix, matplotlib package Font settings under the text class; official document:

(4) Spacing and spacing line parameters between heat map matrix blocks:

linewidths: Define the " The gap size between "matrix patches" that represent pairwise feature relationships
linecolor: The color of the line that divides each matrix patch on the heat map. The default value is 'white'

(5) Heat map color scale bar parameters:

cbar: Whether to draw a color scale bar on the side of the heat map. The default value is True
cbar_kws: When drawing color scale bars on the side of the heat map, the relevant font settings, the default value is None
cbar_ax: When drawing the color scale bars on the side of the heat map, the scale bar position settings, the default value is None

(6) square: Set the shape of the heat map matrix, the default value is False

xticklabels, yticklabels:xticklabels controls each column Output of label names; yticklabels controls the output of label names for each line. The default value is auto. If True, the column name of the DataFrame is used as the label name. If False, no row label names are added. If it is a list, the label name is changed to the content given in the list. If it is an integer K, label every K labels on the graph. If it is auto, the label spacing of the labels will be automatically selected, and the non-overlapping part (or all) of the label names will be output.
mask: Controls whether a certain matrix block is displayed. The default value is None. If it is a Boolean DataFrame, cover the True position in the DataFrame with white
ax: Set the coordinate axis of the drawing. Generally, when drawing multiple subgraphs, you need to modify the coordinates of different subgraphs. Value
**kwargs:All other keyword arguments are passed to ax.pcolormesh

Heat map matrix block color parameters

#cmap(颜色)

import matplotlib.pyplot as plt
% matplotlib inline

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

# 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)
ax1.set_title('cubehelix map')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x轴图例为空值
ax1.set_ylabel('kind')

# cmap用matplotlib colormap
sns.heatmap(pt, linewidths = 0.05, ax = ax2, vmax=900, vmin=0, cmap='rainbow') 
# rainbow为 matplotlib 的colormap名称
ax2.set_title('matplotlib colormap')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')

How to implement visual heat map in python

#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 )
ax1.set_title('center=None')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x轴图例为空值ax1.set_ylabel('kind')# 当center设置小于数据的均值时,生成的图片颜色要向0值代表的颜色一段偏移sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=200)   
ax2.set_title('center=3000')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')

How to implement visual heat map in python

#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 )
ax1.set_title('robust=False')
ax1.set_xlabel('')
ax1.set_xticklabels([]) #设置x轴图例为空值ax1.set_ylabel('kind')

sns.heatmap(pt, linewidths = 0.05, ax = ax2, cmap=cmap, center=None, robust=True ) 
ax2.set_title('robust=True')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')

How to implement visual heat map in python

Heat map matrix block annotation parameters

#annot(矩阵上数字),annot_kws(矩阵上数字的大小颜色字体)matplotlib包text类下的字体设置import numpy as np
np.random.seed(20180316)
x = np.random.randn(4, 4)

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

sns.heatmap(x, annot=True, ax=ax1)

sns.heatmap(x, annot=True, ax=ax2, annot_kws={'size':9,'weight':'bold', 'color':'blue'})# Keyword arguments for ax.text when annot is True.  http://stackoverflow.com/questions/35024475/seaborn-heatmap-key-words

How to implement visual heat map in python

#fmt(字符串格式代码,矩阵上标识数字的数据格式,比如保留小数点后几位数字)import numpy as np
np.random.seed(0)
x = np.random.randn(4,4)

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

sns.heatmap(x, annot=True, ax=ax1)

sns.heatmap(x, annot=True, fmt='.1f', ax=ax2)

How to implement visual heat map in python

热力图矩阵块之间间隔及间隔线参数

#linewidths(矩阵小块的间隔),linecolor(切分热力图矩阵小块的线的颜色)import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize = (6,4))
cmap = sns.cubehelix_palette(start = 1, rot = 3, gamma=0.8, as_cmap = True)   
sns.heatmap(pt, cmap = cmap, linewidths = 0.05, linecolor= 'red', ax = ax)   
ax.set_title('Amounts per kind and region')
ax.set_xlabel('region')
ax.set_ylabel('kind')

How to implement visual heat map in python

#xticklabels,yticklabels横轴和纵轴的标签名输出import matplotlib.pyplot as plt
f, (ax1,ax2) = plt.subplots(figsize = (5,5),nrows=2)

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

p1 = sns.heatmap(pt, ax=ax1, cmap=cmap, center=None, xticklabels=False)
ax1.set_title('xticklabels=None',fontsize=8)

p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, center=None, xticklabels=2, yticklabels=list(range(5))) 
ax2.set_title('xticklabels=2, yticklabels is a list',fontsize=8)
ax2.set_xlabel('region')

How to implement visual heat map in python

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

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

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

p1 = sns.heatmap(pt, ax=ax1, cmap=cmap, xticklabels=False, mask=None)
ax1.set_title('mask=None')
ax1.set_ylabel('kind')

p2 = sns.heatmap(pt, ax=ax2, cmap=cmap, xticklabels=True, mask=(pt<800))   
#mask对pt进行布尔型转化,结果为True的位置用白色覆盖
ax2.set_title(&#39;mask: boolean DataFrame&#39;)
ax2.set_xlabel(&#39;region&#39;)
ax2.set_ylabel(&#39;kind&#39;)

How to implement visual heat map in python

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

f,(ax1,ax2) = plt.subplots(figsize=(4,6),nrows=2)
x = np.array([[1,2,3],[2,0,1],[-1,-2,0]])
sns.heatmap(x, annot=True, ax=ax1)
sns.heatmap(x, mask=x < 1, ax=ax2, annot=True, annot_kws={"weight": "bold"})   #把小于1的区域覆盖掉

How to implement visual heat map in python

The above is the detailed content of How to implement visual heat map in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn