一種將規則化矩陣數據轉換成顏色色調的常用的可視化方法;
每個單元對應數據的某些屬性;
屬性的值通過顏色映射轉換為不同色調並填充規則單元;
表格坐標的排列和順序都可以通過參數控制;
適合的坐標排列和順序可以很好地發現數據的不同性質;
繪製熱力圖樹形圖(dendrogram)表示連續合併的每對磊之間的屬性距離的示意圖;
示意圖將以圖形的方式進行排布;
使得要合併的每對類的成員在示意圖中相鄰;
樹形圖工具採用層次聚類算法;
先計算輸入的特徵文件中每對類之間的距離;
再迭代式地合併最近的一對類;
完成後續合併下一對最近的類;
直到合併完所有的類;
在每次合併後,每對類之間的距離會進行更新;
合併類特徵時採用的距離將用於構建樹形圖;
樹形圖分類:繪製橫向樹形圖SciPy包的dendrogram()函數可以根據數據繪製樹形圖;
其中當orientation='top'時,繪製縱向樹形圖;
參數orientation='left'時,繪製橫向樹形圖;
熱力圖import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from plotnine.data import mtcars
from sklearn.preprocessing import scale
sns.set_style("white")
sns.set_context("notebook", font_scale=1.5,
rc={'axes.labelsize': 17, 'legend.fontsize':17,
'xtick.labelsize': 15,'ytick.labelsize': 10})
df=mtcars.set_index('name')
df.loc[:,:] = scale(df.values )
#熱力圖
fig=plt.figure(figsize=(7, 7),dpi=80)
sns.heatmap(df, center=0, cmap="RdYlBu_r",
linewidths=.15,linecolor='k')
#sns.set()
plt.savefig('heatmap2.pdf')
帶層次聚類的熱力圖import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from plotnine.data import mtcars
from sklearn.preprocessing import scale
sns.set_style("white")
sns.set_context("notebook", font_scale=1.5,
rc={'axes.labelsize': 17, 'legend.fontsize':17,
'xtick.labelsize': 15,'ytick.labelsize': 10})
df=mtcars.set_index('name')
df.loc[:,:] = scale(df.values )
#帶層次聚類的熱力圖.
sns.clustermap(df, center=0, cmap="RdYlBu_r",
linewidths=.15,linecolor='k', figsize=(8, 8))
plt.savefig('heatmap1.pdf')
橫向樹形圖import scipy.cluster.hierarchy as shc
import numpy as np
from matplotlib import cm,colors
from matplotlib import pyplot as plt
import pandas as pd
from plotnine.data import mtcars
from sklearn.preprocessing import scale
plt.rcParams['axes.facecolor']='w'
plt.rc('axes',axisbelow=True)
df=mtcars.set_index('name')
df.loc[:,:] = scale(df.values )
#橫向樹形圖
fig=plt.figure(figsize=(10, 10), dpi= 80)
dend = shc.dendrogram(shc.linkage(df,method='ward'), orientation='left',
labels=df.index.values, color_threshold=5
)
plt.xticks(fontsize=13,rotation=0)
plt.yticks(fontsize=14)
ax = plt.gca()
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('k')
ax.spines['bottom'].set_color('k')
#plt.savefig('樹狀圖1.pdf')
plt.show()
縱向樹形圖import scipy.cluster.hierarchy as shc
import numpy as np
from matplotlib import cm,colors
from matplotlib import pyplot as plt
import pandas as pd
from plotnine.data import mtcars
from sklearn.preprocessing import scale
plt.rcParams['axes.facecolor']='w'
plt.rc('axes',axisbelow=True)
df=mtcars.set_index('name')
df.loc[:,:] = scale(df.values )
#縱向樹形圖
fig=plt.figure(figsize=(5, 5), dpi= 80)
dend = shc.dendrogram(shc.linkage(df.values.T,method='ward'), orientation='top',
labels=df.columns.values, color_threshold=5,
)
plt.xticks(fontsize=13,rotation=0)
plt.yticks(fontsize=14)
ax = plt.gca()
ax.spines['left'].set_color('k')
ax.spines['right'].set_color('k')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_color('none')
#plt.savefig('樹狀圖2.pdf')
plt.show()