房价下拉菜单
您在一家房地产投资公司担任数据分析师。公司希望了解过去 5 年悉尼几个关键郊区的房价回报情况。您的工作将补充这些郊区的定性分析。他们希望既能单独查看每个郊区,又能在它们之间轻松切换。
他们向您提供了这些郊区在 2015 年和 2020 年的价格数据。
在本练习中,您的任务是为这组数据创建折线图,并添加一个下拉菜单以选择各个郊区。此外,他们指出其中一个郊区增长强劲,因此您只想为该条迹添加注释。挑战接受!
您已经有一个 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': []}]},
]