這篇文章主要介紹了python如何實作可視化熱力圖,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟随小編過來看看吧
熱力圖
1、利用熱力圖可以看資料表裡多個特征兩兩的相似度。參考官方API參數及位址:
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)熱力圖輸入資料參數:
data:矩陣資料集,可以是numpy的數組(array),也可以是pandas的DataFrame。如果是DataFrame,則df的index/column資訊會分别對應到heatmap的columns和rows,即pt.index是熱力圖的行标,pt.columns是熱力圖的列标
(2)熱力圖矩陣塊顔色參數:
vmax,vmin:分别是熱力圖的顔色取值最大和最小範圍,預設是根據data資料表裡的取值确定
cmap:從數字到色彩空間的映射,取值是matplotlib包裡的colormap名稱或顔色對象,或者表示顔色的清單;改參數預設值:根據center參數設定
center:資料表取值有差異時,設定熱力圖的色彩中心對齊值;通過設定center值,可以調整生成的圖像顔色的整體深淺;設定center資料時,如果有資料溢出,則手動設定的vmax、vmin會自動改變
robust:預設取值False;如果是False,且沒設定vmin和vmax的值,熱力圖的顔色映射範圍根據具有魯棒性的分位數設定,而不是用極值設定
(3)熱力圖矩陣塊注釋參數:
annot(annotate的縮寫):預設取值False;如果是True,在熱力圖每個方格寫入資料;如果是矩陣,在熱力圖每個方格寫入該矩陣對應位置資料
fmt:字元串格式代碼,矩陣上辨別數字的資料格式,比如保留小數點後幾位數字
annot_kws:預設取值False;如果是True,設定熱力圖矩陣上數字的大小顔色字型,matplotlib包text類下的字型設定;官方文檔:
(4)熱力圖矩陣塊之間間隔及間隔線參數:
linewidths:定義熱力圖裡“表示兩兩特征關系的矩陣小塊”之間的間隔大小
linecolor:切分熱力圖上每個矩陣小塊的線的顔色,預設值是’white’
(5)熱力圖顔色刻度條參數:
cbar:是否在熱力圖側邊繪制顔色刻度條,預設值是True
cbar_kws:熱力圖側邊繪制顔色刻度條時,相關字型設定,預設值是None
cbar_ax:熱力圖側邊繪制顔色刻度條時,刻度條位置設定,預設值是None
(6)square:設定熱力圖矩陣小塊形狀,預設值是False
xticklabels, yticklabels:xticklabels控制每列标簽名的輸出;yticklabels控制每行标簽名的輸出。預設值是auto。如果是True,則以DataFrame的列名作為标簽名。如果是False,則不添加行标簽名。如果是清單,則标簽名改為清單中給的内容。如果是整數K,則在圖上每隔K個标簽進行一次标注。 如果是auto,則自動選擇标簽的标注間距,将标簽名不重疊的部分(或全部)輸出
mask:控制某個矩陣塊是否顯示出來。預設值是None。如果是布爾型的DataFrame,則将DataFrame裡True的位置用白色覆寫掉
ax:設定作圖的坐标軸,一般畫多個子圖時需要修改不同的子圖的該值
**kwargs:All other keyword arguments are passed to ax.pcolormesh
熱力圖矩陣塊顔色參數#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')

#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')
#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')
熱力圖矩陣塊注釋參數#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
#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)
熱力圖矩陣塊之間間隔及間隔線參數#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')
#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')
#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('mask: boolean DataFrame')
ax2.set_xlabel('region')
ax2.set_ylabel('kind')
用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的區域覆寫掉
以上就是python如何實作可視化熱力圖的詳細内容,更多請關注php中文網其它相關文章!
本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!