当前位置:网站首页>Heat mapping using Seaborn

Heat mapping using Seaborn

2022-06-21 13:05:00 51CTO


In addition to statistical charts ,seaborn You can also draw a heat map , And support the drawing of cluster tree , There are two functions for plotting heat maps

1. heatmap,  Draw a normal heat map

2. clustermap, Draw the heat map with cluster number

1. heatmap

comparison matplotlib Of imshow function , This function provides a more concise interface , You can easily add text notes and other functions , The basic usage is as follows

      
      
>>> import numpy as np
>>> data = np.random.rand(10, 10)
>>> sns.heatmap(data)
>>> plt.show()
  • 1.
  • 2.
  • 3.
  • 4.

The output is as follows

 Use seaborn Draw a heat map _ Split line

stay imshow Some of the parameters in can also be used in this function , such as vmin, vmax,cmap Equal parameter . In addition to general parameters , This function has two features , The first is that you can easily add split lines , Make the picture more beautiful , Use linescolor and linewidth Parameter specifies the color and width of the split line , Usage is as follows

      
      
>>> sns.heatmap(data, linewidth=1)
>>> plt.show()
  • 1.
  • 2.

The output is as follows

 Use seaborn Draw a heat map _ Split line _02

The second feature is the addition of numeric annotations , Display the corresponding value on the cell , Usage is as follows

      
      
>>> sns.heatmap(data, linewidth=1, annot=True)
>>> plt.show()
  • 1.
  • 2.

The output is as follows

 Use seaborn Draw a heat map _ Data analysis _03

2. clustermap

clustermap Draw the heat map with cluster number , The basic usage is as follows

      
      
>>> data = np.random.rand(10,5)
>>> df = pd.DataFrame(data)
>>> df.columns = ['sampleA', 'sampleB', 'sampleC', 'sampleD', 'sampleE']
>>> sns.clustermap(df)
>>> plt.show()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

The output is as follows

 Use seaborn Draw a heat map _ Split line _04

The clustering tree in the figure is through scipy The distance matrix and clustering algorithm provided in the module , adopt method and metrix Parameters can specify clustering algorithm and distance matrix algorithm respectively .

For visualization , The following parameters are commonly used 3 individual , first standard_scale,  Standardize the data , For example, standardize by line , Usage is as follows

      
      
>>> sns.clustermap(df, standard_scale=0)
>>> plt.show()
  • 1.
  • 2.

The output is as follows

 Use seaborn Draw a heat map _ Split line _05

The second parameter is z_score, zscore It is also a means of data standardization , Calculated by row and column zscore Can be used as follows

      
      
>>> sns.clustermap(df, z_score=0)
>>> plt.show()
  • 1.
  • 2.

The output is as follows

 Use seaborn Draw a heat map _ clustering _06

The third parameter is zero row_color/col_color.  Used to annotate row labels and column labels , Usage is as follows

      
      
>>> sns.clustermap(df, col_colors=['r','g','b','b','b'])
>>> plt.show()
  • 1.
  • 2.

The output is as follows

 Use seaborn Draw a heat map _ clustering _07

The above just introduces the basic usage and common parameters of the two functions , In fact, there are many specific parameters , You can use the on the official website API Documentation to learn the usage of each parameter in detail .

·end·

 Use seaborn Draw a heat map _ Split line _08

A man who only shares dry goods

Shengxin official account


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211232346001.html