時間按鈕

Python 的 Plotly 資料視覺化入門

Alex Scriven

Data Scientist

什麼是時間按鈕?

 

  • 讓折線圖可篩選/縮放
    • 1D = 最近 1 天的資料
    • 1M = 最近 1 個月
    • 1Y = 最近 1 年
    • YTD = 年初至今

 

Yahoo 財經按鈕

Python 的 Plotly 資料視覺化入門

Plotly 的時間按鈕

 

包含幾個關鍵元素的字典:

  • label = 按鈕上顯示的文字
  • count = 點擊時移動多少個 step
  • step = 要移動的期間('month''year''day'
  • stepmode = 'backward''todate'
    • 'backward' = 直接往回移動 count 個單位
    • 'todate' = 類似 'backward',但四捨五入到下一個完整期間的起始點
Python 的 Plotly 資料視覺化入門

「backward」與「todate」

 

  • 資料集結束於 10 月 20 日,且有一個 6 個月按鈕(count=6step='month'):
    • stepmode='backward' 會把圖縮放到從「4 月 20 日」開始(往回 6 個月)
    • stepmode='todate' 會把圖縮放到從「5 月 1 日」開始(4 月 20 日的下一個整月起始日)
Python 的 Plotly 資料視覺化入門

雪梨降雨範例

 

  • 按鈕以字典清單指定
date_buttons = [
{'count': 6, 'step': "month", 'stepmode': "todate", 'label': "6MTD"},

{'count': 14, 'step': "day", 'stepmode': "todate", 'label': "2WTD"} ]
Python 的 Plotly 資料視覺化入門

加入時間按鈕

fig = px.line(data_frame=rain, x='Date', 
        y='Rainfall', 
        title="Rainfall (mm) in Sydney")

fig.update_layout(dict( xaxis=dict( rangeselector=dict(buttons=date_buttons) ))) fig.show()

帶按鈕的折線圖

Python 的 Plotly 資料視覺化入門

點擊時間按鈕

 

點擊 2WTD 按鈕:

過去兩週降雨

 

點擊 6MTD 按鈕:

過去 6 個月降雨

Python 的 Plotly 資料視覺化入門

一起來練習吧!

Python 的 Plotly 資料視覺化入門

Preparing Video For Download...