LoslegenKostenlos loslegen

Rainfall by season slider

The Australian Bureau of Meteorology has contracted you for some follow-up work on the visualization they loved on rainfall per month in Sydney.

They would love the ability to cycle through seasons. Not as a change-between as in buttons and dropdowns, but more of a slide-between.

Sounds like the perfect job for a slider!

In this exercise, you are tasked with creating a bar chart of the rainfall data with a slider through the seasons.

You have a rain_pm DataFrame available.

Diese Übung ist Teil des Kurses

Introduction to Data Visualization with Plotly in Python

Kurs anzeigen

Anleitung zur Übung

  • Label each slider step using the correct season, in the order they were looped.
  • Set trace visibility for each slider step to show only the correct season.
  • Add the slider to the figure layout.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

fig = go.Figure()

for season in ['Autumn', 'Winter', 'Spring']:
    df = rain_pm[rain_pm.Season == season]
    fig.add_trace(px.bar(df, x="Month", y="Total Rainfall", title=season).data[0])
    
# Create the slider elements
sliders = [
    {'steps':[
    {'method': 'update', 'label': '____', 'args': [{'visible': [____, ____, ____]}]},
    {'method': 'update', 'label': '____', 'args': [{'visible': [____, ____, ____]}]},
    {'method': 'update', 'label': '____', 'args': [{'visible': [____, ____, ____]}]}]}]

# Add the slider to the figure 
fig.update_layout({'____': ____})

fig.show()
Code bearbeiten und ausführen