雙變量視覺化

Python 的 Plotly 資料視覺化入門

Alex Scriven

Data Scientist

什麼是雙變量視覺化?

  • 呈現兩個變數的比較

$$

$$

  • 散佈圖
  • 折線圖
  • 相關係數圖
Python 的 Plotly 資料視覺化入門

散佈圖

  • y 軸代表一個變數
  • x 軸代表另一個變數
  • 每個交會點是一個點,例如 (68, 472)

scatterplot

Python 的 Plotly 資料視覺化入門

使用 plotly.express 畫散佈圖

 

import plotly.express as px

fig = px.scatter( data_frame=penguins, x="Body Mass (g)", y="Flipper Length (mm)") fig.show()

企鵝散佈圖

Python 的 Plotly 資料視覺化入門

更多 plotly.express 參數

 

實用的 plotly.express 散佈圖參數:

  • trendline:加入各種趨勢線
  • symbol:為不同類別設定不同符號

$$

$$

$$

更多請見 文件

Python 的 Plotly 資料視覺化入門

plotly.express 中的折線圖

  • 用來呈現隨時間變化的變數

$$

fig = px.line(
  data_frame=msft_stock,  
  x="Date", 
  y="Open", 
  title="MSFT Stock Price (5Y)")
fig.show()

$$

股票價格的簡單折線圖

Python 的 Plotly 資料視覺化入門

相關係數圖

  • 視覺化多個變數之間的相關性

$$

皮爾森相關係數:

  • 值介於 -1 到 1
  • 1 表示完全正相關
  • 0 表示無相關
  • -1 表示完全負相關
Python 的 Plotly 資料視覺化入門

相關係數圖前置

 

df 包含單車租借量與天氣條件的資料

cr = df.corr(method="pearson")
print(cr)

 

相關係數表

Python 的 Plotly 資料視覺化入門

用 Plotly 畫相關係數圖

$$

fig = px.imshow(

cr,
text_auto=True,
color_continuous_scale="RdYlGn", zmin=-1, zmax=1)
fig.show()
Python 的 Plotly 資料視覺化入門

我們的相關係數圖

熱度圖範例

Python 的 Plotly 資料視覺化入門

一起來練習吧!

Python 的 Plotly 資料視覺化入門

Preparing Video For Download...