Table of Contents
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:
(1) Heat map input data parameters:
Heat map matrix block color parameters
Heat map matrix block annotation parameters
(4) Spacing and spacing line parameters between heat map matrix blocks:
(5) Heat map color scale bar parameters:
热力图矩阵块之间间隔及间隔线参数
用mask实现:突出显示某些数据
Home Backend Development Python Tutorial How to implement visual heat map in python

How to implement visual heat map in python

Apr 04, 2018 pm 02:22 PM
python Visualization heat map


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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#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')

Copy after login

How to implement visual heat map in python

1

2

3

4

5

6

7

8

9

10

#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')

Copy after login

How to implement visual heat map in python

1

2

3

4

5

6

7

8

9

10

11

12

13

#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')

Copy after login

How to implement visual heat map in python

Heat map matrix block annotation parameters

1

2

3

4

5

6

7

8

9

#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

Copy after login

How to implement visual heat map in python

1

2

3

4

5

6

7

8

9

#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)

Copy after login

How to implement visual heat map in python

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

1

2

3

4

5

6

7

#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')

Copy after login

How to implement visual heat map in python

1

2

3

4

5

6

7

8

9

10

11

#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')

Copy after login

How to implement visual heat map in python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#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;)

Copy after login

How to implement visual heat map in python

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

1

2

3

4

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的区域覆盖掉

Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

See all articles