房價下拉選單
你在一家不動產投資公司擔任資料分析師。公司希望你協助了解過去 5 年雪梨幾個重點郊區的房價報酬。你的分析會補充這些郊區的質性評估。他們想能夠分別視覺化各郊區,但也能輕鬆在它們之間切換。
他們提供了這些郊區在 2015 與 2020 的房價資料。
在這個練習中,你要用這些資料建立折線圖,並加上一個下拉選單來選取各郊區。此外,他們指出其中一個郊區成長特別亮眼,所以你只想替那個 trace 加上註解。你接受挑戰!
你已經有一個 house_prices 的 DataFrame,可使用的 go.Figure() 物件也會替你建立好。
本練習屬於課程
Python 的 Plotly 資料視覺化入門
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the figure
fig = go.Figure()
# Loop through the suburbs
for suburb in ['Greenacre', 'Lakemba']:
# Subset the DataFrame
df = house_prices[house_prices.Suburb == suburb]
# Add a trace for each suburb subset
fig.add_trace(px.line(df, x='Year', y='Median House Price').data[0])
# Annotation
ga_annotation=[{'text': 'Price boom!', 'showarrow': True, 'x': 'Year: 2018', 'y': 712678}]
# Create the buttons
dropdown_buttons = [
{'label': "Greenacre", 'method': "update", 'args': [{"visible": [____, ____]}, {'title': 'Greenacre', 'annotations': ____}]},
{'label': "Lakemba", 'method': "update", 'args': [{"visible": [____, ____]}, {'title': 'Lakemba', 'annotations': []}]},
]