在降雨量图上添加时间按钮
当地新闻台想更新其网站天气板块的图表。他们联系您,协助改造旧有的图片和表格。
他们需要一张折线图,并能筛选最近 4 周(4WTD)、过去 48 小时(48HR)以及年初至今(YTD)的数据。
在本练习中,您将帮助新闻台构建带有上述按钮的折线图。
已提供包含所需数据的 rain DataFrame。
本练习是课程的一部分
Python 中的 Plotly 数据可视化入门
练习说明
- 按上述名称创建按钮列表。
- 使用
update_layout()更新图形,并用刚创建的列表来构建按钮。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create the buttons
date_buttons = [
{'count': ____, 'label': "____", 'step': "day", 'stepmode': "todate"},
{'count': ____, 'label': "____", 'step': "hour", 'stepmode': "todate"},
{'count': ____, 'label': "____", 'step': "year", 'stepmode': "todate"}]
# Create the line chart
fig = px.line(rain, x='Date', y='Rainfall',
title= "Rainfall (mm)")
# Add the buttons
fig.update_layout(dict(
xaxis=dict(
____=dict(buttons=____)
)))
fig.show()