多層次繪圖

Python 的 Plotly 資料視覺化入門

Alex Scriven

Data Scientist

什麼是圖層疊加?

  • 在同一張圖上疊加多個圖

$$

GDP layered plot

  • 不需分開的格線位置
  • 使用 add_trace()

$$

$$

  • 不需分開的格線位置
  • 使用 add_trace()
Python 的 Plotly 資料視覺化入門

為什麼要疊加?

 

  • 取得更多自訂彈性
    • 例:疊加多條折線圖

$$

  • 顯示互補的圖形類型

$$

  • 用不同圖形引導注意力

$$

  • 更細緻比較資料
Python 的 Plotly 資料視覺化入門

長條+折線疊加圖

 

  • 讓你在同一視圖比較精確數值與整體趨勢

Layered plot example

Python 的 Plotly 資料視覺化入門

GDP 成長疊加圖

from plotly.graph_objects import Figure

layered_fig = Figure()

bar_fig = px.bar(df, x='Date' , y='Quarterly Growth (%)')
line_fig = px.line(df, x='Date' , y='Rolling YTD Growth (%)' , color_discrete_sequence=['red'])
layered_fig = Figure(data= [*bar_fig.data, *line_fig.data]) layered_fig.show()

 

GDP layered plot

Python 的 Plotly 資料視覺化入門

加入更多圖層

 

# Code the same bar_fig, line_fig
line_fig2 = px.line(df, x='Date'
    , y='OECD Average GDP Growth (%)'
    , color_discrete_sequence=['green'])

layered_fig = Figure(data= [*bar_fig.data , *line_fig.data, *line_fig2.data]) layered_fig.show()

 

GDP layered plot

Python 的 Plotly 資料視覺化入門

稍後再加更多圖層

 

# Create with 2 figures
layered_fig = Figure(data=[*bar_fig.data, *line_fig.data])

# Add last line layered_fig.add_trace(line_fig2.data[0])
Python 的 Plotly 資料視覺化入門

一起來練習吧!

Python 的 Plotly 資料視覺化入門

Preparing Video For Download...