CommencerCommencer gratuitement

Time buttons on our rainfall graph

The local news station wants to update the graphics in the weather section of its 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), the previous 48 hours (48HR), and the year to date (YTD).

In this exercise, you will help the news station build its line chart with the requested buttons.

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

Cet exercice fait partie du cours

Introduction to Data Visualization with Plotly in Python

Afficher le cours

Instructions

  • Create the list of buttons specified with the names noted above.
  • Update the figure using update_layout() to construct buttons using the list you just created.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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 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()
Modifier et exécuter le code