LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Introduction to Data Visualization with Plotly in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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()
Code bearbeiten und ausführen