Get startedGet started for free

Time buttons on our rainfall graph

The local news station is wanting to update the graphics in the weather section of their website. They have contacted you to assist in jazzing up the old images and tables they have.

They have requested a line chart, but with the ability to filter the data for the last 4 weeks (4WTD), last 48 hours (48HR) and the year to date (YTD).

In this exercise, you will help the news station by building their line chart with the requested buttons.

You have a rain DataFrame available that contains the necessary data.

This exercise is part of the course

Introduction to Data Visualization with Plotly in Python

View Course

Exercise instructions

  • Create the list of buttons specified with the names noted above.
  • Create the basic line chart using the rain DataFrame, appropriately using the Date and Rainfall columns.
  • Update the figure using update_layout() to construct buttons using the list you just created.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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 basic line chart
fig = px.line(data_frame=____, x=____, y=____, 
              title="Rainfall (mm)")

# Add the buttons and show
fig.update_layout(
  	{'xaxis':
    {____: {'buttons': date_buttons}}})
fig.show()
Edit and Run Code