開始使用免費開始

降雨量圖形類型按鈕

澳洲氣象局來請你協助分析每月降雨量。

他們不確定哪一種圖最能呈現這份資料,因此想把圖形類型做成互動切換。他們不想用子圖,因為一次只需要看一種圖。

你的任務是建立每月降雨量的長條圖,並提供一個按鈕,讓使用者可以在同一份資料的長條圖與散佈圖之間輕鬆切換。

已為你載入 rain DataFrame。

本練習屬於課程

Python 的 Plotly 資料視覺化入門

檢視課程

練習說明

  • 建立長條圖與散佈圖的按鈕,使用圖形 type 參數並傳入 args
  • 把按鈕加入圖中,將 direction 設為 down,讓按鈕上下疊放。

動手互動練習

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

# Create a bar chart
fig = px.bar(rain, x='Month', y='Rainfall')

# Create the buttons
my_buttons = [{'label': "Bar plot", 'method': "update", 'args': [{"type": "____"}]},
{'label': "scatterplot", 'method': "update", 'args': [{"type": "____", 'mode': 'markers'}]}]

# Add buttons to the plot and show
fig.update_layout({
    ____: [{
      'type': "buttons", 'direction':  ____,
      'x': 1.3, 'y': 0.5,
      'showactive': True, 'active': 0,
      'buttons': ____}]})
fig.show()
編輯並執行程式碼