開始使用免費開始

叢集熱圖

熱圖非常適合用來視覺化相關係數矩陣,不過叢集熱圖更勝一籌。Clustermap 會對相關係數矩陣做階層式叢集,並產生對應的熱圖,讓你看出其中的結構:

df_corr = df.corr()

fig = sns.clustermap(df_corr)
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)

為了避免座標軸標籤重疊,你可以從底層的 fig 物件參照 Axes,並指定旋轉角度。你可以在這裡了解 clustermap() 函式的參數說明:here

本練習屬於課程

使用 Python 視覺化時間序列資料

檢視課程

練習說明

  • 匯入 seaborn 並命名為 sns
  • 使用 Pearson 方法計算 meat DataFrame 各欄位兩兩之間的相關係數,並將結果指定給新變數 corr_meat
  • 繪製 corr_meat 的叢集熱圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import seaborn library
____

# Get correlation matrix of the meat DataFrame
corr_meat = ____(____)

# Customize the heatmap of the corr_meat correlation matrix and rotate the x-axis labels
fig = ____(corr_meat,
                     row_cluster=True,
                     col_cluster=True,
                     figsize=(10, 10))

plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
plt.show()
編輯並執行程式碼