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)
data:矩陣數(shù)據(jù)集,可以是numpy的數(shù)組(array),也可以是pandas的DataFrame。如果是DataFrame,則df的index/column信息會分別對應(yīng)到heatmap的columns和rows,即pt.index是熱力圖的行標(biāo),pt.columns是熱力圖的列標(biāo)
vmax,vmin:分別是熱力圖的顏色取值最大和最小范圍,默認(rèn)是根據(jù)data數(shù)據(jù)表里的取值確定
cmap:從數(shù)字到色彩空間的映射,取值是matplotlib包里的colormap名稱或顏色對象,或者表示顏色的列表;改參數(shù)默認(rèn)值:根據(jù)center參數(shù)設(shè)定
center:數(shù)據(jù)表取值有差異時,設(shè)置熱力圖的色彩中心對齊值;通過設(shè)置center值,可以調(diào)整生成的圖像顏色的整體深淺;設(shè)置center數(shù)據(jù)時,如果有數(shù)據(jù)溢出,則手動設(shè)置的vmax、vmin會自動改變
robust:默認(rèn)取值False;如果是False,且沒設(shè)定vmin和vmax的值,熱力圖的顏色映射范圍根據(jù)具有魯棒性的分位數(shù)設(shè)定,而不是用極值設(shè)定
annot(annotate的縮寫):默認(rèn)取值False;如果是True,在熱力圖每個方格寫入數(shù)據(jù);如果是矩陣,在熱力圖每個方格寫入該矩陣對應(yīng)位置數(shù)據(jù)
fmt:字符串格式代碼,矩陣上標(biāo)識數(shù)字的數(shù)據(jù)格式,比如保留小數(shù)點后幾位數(shù)字
annot_kws:默認(rèn)取值False;如果是True,設(shè)置熱力圖矩陣上數(shù)字的大小顏色字體,matplotlib包text類下的字體設(shè)置;官方文檔:
linewidths:定義熱力圖里“表示兩兩特征關(guān)系的矩陣小塊”之間的間隔大小
linecolor:切分熱力圖上每個矩陣小塊的線的顏色,默認(rèn)值是’white’
cbar:是否在熱力圖側(cè)邊繪制顏色刻度條,默認(rèn)值是True
cbar_kws:熱力圖側(cè)邊繪制顏色刻度條時,相關(guān)字體設(shè)置,默認(rèn)值是None
cbar_ax:熱力圖側(cè)邊繪制顏色刻度條時,刻度條位置設(shè)置,默認(rèn)值是None
xticklabels, yticklabels:xticklabels控制每列標(biāo)簽名的輸出;yticklabels控制每行標(biāo)簽名的輸出。默認(rèn)值是auto。如果是True,則以DataFrame的列名作為標(biāo)簽名。如果是False,則不添加行標(biāo)簽名。如果是列表,則標(biāo)簽名改為列表中給的內(nèi)容。如果是整數(shù)K,則在圖上每隔K個標(biāo)簽進(jìn)行一次標(biāo)注。 如果是auto,則自動選擇標(biāo)簽的標(biāo)注間距,將標(biāo)簽名不重疊的部分(或全部)輸出
mask:控制某個矩陣塊是否顯示出來。默認(rèn)值是None。如果是布爾型的DataFrame,則將DataFrame里True的位置用白色覆蓋掉
ax:設(shè)置作圖的坐標(biāo)軸,一般畫多個子圖時需要修改不同的子圖的該值
**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([]) #設(shè)置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([]) #設(shè)置x軸圖例為空值ax1.set_ylabel('kind')# 當(dāng)center設(shè)置小于數(shù)據(jù)的均值時,生成的圖片顏色要向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([]) #設(shè)置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(矩陣上數(shù)字),annot_kws(矩陣上數(shù)字的大小顏色字體)matplotlib包text類下的字體設(shè)置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(字符串格式代碼,矩陣上標(biāo)識數(shù)字的數(shù)據(jù)格式,比如保留小數(shù)點后幾位數(shù)字)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橫軸和縱軸的標(biāo)簽名
#mask對某些矩陣塊的顯示進(jìn)行覆蓋 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進(jìn)行布爾型轉(zhuǎn)化,
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的區(qū)域覆蓋掉
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com