開始使用免費開始

在降雨圖上加入時間按鈕

當地電視台想更新其網站天氣專區的圖表。他們聯絡你,協助把原本的舊圖片與表格改得更吸引人。

他們需要一張折線圖,並且能夠篩選最近 4 週(4WTD)、過去 48 小時(48HR),以及今年迄今(YTD)。

在這個練習中,你會協助電視台建立這張折線圖,並加上所需的按鈕。

你已經有一個包含必要資料的 rain DataFrame 可供使用。

本練習屬於課程

Python 的 Plotly 資料視覺化入門

檢視課程

練習說明

  • 依照上面指定的名稱建立按鈕清單。
  • 使用 update_layout() 更新圖表,並用你剛建立的清單來建立按鈕。

動手互動練習

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

# Create the buttons
date_buttons = [
{'count': ____, 'label': "____", 'step': "day", 'stepmode': "todate"},
{'count': ____, 'label': "____", 'step': "hour", 'stepmode': "todate"},
{'count': ____, 'label': "____", 'step': "year", 'stepmode': "todate"}]

# Create the line chart
fig = px.line(rain, x='Date', y='Rainfall', 
              title= "Rainfall (mm)")

# Add the buttons
fig.update_layout(dict(
    xaxis=dict(
        ____=dict(buttons=____)
    )))

fig.show()
編輯並執行程式碼