開始使用免費開始

視覺化相關係數矩陣

在上一個練習中產生的相關係數矩陣,可以用熱圖來呈現。你可以運用 seaborn 函式庫中的 heatmap() 函式,並透過多種參數來調整熱圖的外觀。

df_corr = df.corr()

sns.heatmap(df_corr)
plt.xticks(rotation=90)
plt.yticks(rotation=0) 

你可以使用 .xticks().yticks() 方法旋轉座標軸標籤,避免彼此重疊。

想了解 heatmap() 可用的參數,請參考這個[頁面](https://seaborn.pydata.org/generated/seaborn.heatmap.html)。

本練習屬於課程

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

檢視課程

練習說明

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

動手互動練習

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

# Import seaborn library
import ____ as ____

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

# Customize the heatmap of the corr_meat correlation matrix
____(corr_meat,
            annot=True,
            linewidths=0.4,
            annot_kws={"size": 10})

plt.xticks(rotation=90)
plt.yticks(rotation=0) 
plt.show()
編輯並執行程式碼