降雨量图表类型按钮
澳大利亚气象局联系了您,想更好地理解每月降雨量。
他们不确定哪种图表最适合可视化这些数据,因此希望把图表类型作为交互的一部分进行切换。他们不想使用子图,因为一次只需要查看一种图表。
您的任务是创建一个按月份统计降雨量的柱状图,并添加一个按钮,让用户可以在同一数据的柱状图与散点图之间便捷切换。
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()