开始使用免费开始使用

增长地点下拉菜单

澳大利亚政府希望了解哪些地方政府区域(LGA)最近人口增长强劲,以便规划基础设施项目。

他们向您提供了按人口增幅百分比(2018 年至 2019 年)排名前 5 的 LGA 数据,并请您进行可视化展示。不过,他们希望既能筛选某个州,也能一次性查看全部。

在本练习中,您的任务是为这些数据创建一个柱状图,并添加一个下拉菜单,用于在不同州之间切换,或一次性查看所有州。

您已拥有一个 pop_growth DataFrame,并且会为您准备好一个 go.Figure() 对象。

本练习是课程的一部分

Python 中的 Plotly 数据可视化入门

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Create the figure
fig = go.Figure()

# Loop through the states
for state in ['NSW', 'QLD', 'VIC']:
  	# Subset the DataFrame
    df = pop_growth[pop_growth.State == state]
    # Add a trace for each state subset
    fig.add_trace(px.bar(df, x='Local Government Area', y='Change %').data[0])

# Create the buttons
dropdown_buttons = [
{'label': "ALL", 'method': "____", 'args': [{"visible": [True, True, True]}, {"title": "ALL"}]},
{'label': "NSW", 'method': "____", 'args': [{"visible": [____, ____, ____]}, {"title": "NSW"}]},
{'label': "QLD", 'method': "____", 'args': [{"visible": [____, ____, ____]}, {"title": "QLD"}]},
{'label': "VIC", 'method': "____", 'args': [{"visible": [____, ____, ____]}, {"title": "VIC"}]},
]
编辑并运行代码