LoslegenKostenlos loslegen

Rainfall plot type buttons

You have been contacted by the Australian Bureau of Meteorology to assist them in understanding monthly rainfall.

They are unsure what plot would best visualize this data, so they are wondering if there is a way to make this part of the interactivity. They don't want subplots, as they will only need to view one plot at a time.

Your task is to create a bar plot of rainfall per month with a button that allows the user to easily change from a bar plot to a scatterplot of the same data.

A rain DataFrame is loaded for you.

Diese Übung ist Teil des Kurses

Introduction to Data Visualization with Plotly in Python

Kurs anzeigen

Anleitung zur Übung

  • Create a button for the bar plot and scatterplot using the plot type argument fed to args.
  • Add the buttons to the plot, setting the direction to down so the buttons are on top of each other.

Interaktive Übung

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

# Create a bar chart
fig = px.bar(rain, x='Month', y='Rainfall')

# Create the buttons
my_buttons = [{'label': "Bar plot", 'method': "update", 'args': [{"type": "____"}]},
{'label': "scatterplot", 'method': "update", 'args': [{"type": "____", 'mode': 'markers'}]}]

# Add buttons to the plot and show
fig.update_layout({
    ____: [{
      'type': "buttons", 'direction':  ____,
      'x': 1.3, 'y': 0.5,
      'showactive': True, 'active': 0,
      'buttons': ____}]})
fig.show()
Code bearbeiten und ausführen