用按鈕切換註解
某大型電商公司的業務部門希望你協助理解並視覺化他們的資料。
他們提供了每月的銷售資料,但用不同的指標來衡量月績效:每月銷售金額(美元)與銷售量(售出件數)。
他們想把這些資訊放在同一張圖上,並能輕鬆切換註解的顯示與隱藏,好促進討論。
在這個練習中,你會用按鈕來開啟或關閉關鍵註解,幫助業務部門分析他們的銷售資料。
已提供 sales 資料框,且會為你建立一個 go.Figure() 物件。
本練習屬於課程
Python 的 Plotly 資料視覺化入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the figure
fig = Figure()
# Create and add traces (done for you)
fig.add_trace(px.bar(sales, x="Month", y="Sales Volume", color_discrete_sequence=['blue']).data[0])
fig.add_trace(px.bar(sales, x="Month", y="Sales Value", color_discrete_sequence=['red']).data[0])
# Create annotations
value_annotations=[{'text': 'Sept was the best', 'showarrow': True, 'x': '____', 'y': 345397}]
volume_annotations=[{'text': 'Oct was the best', 'showarrow': True, 'x': '____', 'y': 71900}]
# Create buttons
my_buttons = [
{'label': "By Sales Value", 'method': "update", 'args': [{}, {'____': ____}]},
{'label': "By Sales Volume", 'method': "update", 'args': [{}, {'____': ____}]}]