Session Ready
Exercise

Adding a slider to vary the year

Until now, we've been plotting data only for 1970. In this exercise, you'll add a slider to your plot to change the year being plotted. To do this, you'll create an update_plot() function and associate it with a slider to select values between 1970 and 2010.

After you are done, you may have to scroll to the right to view the entire plot. As you play around with the slider, notice that the title of the plot is not updated along with the year. This is something you'll fix in the next exercise!

Instructions
100 XP
  • Import the widgetbox and row functions from bokeh.layouts, and the Slider function from bokeh.models.
  • Define the update_plot callback function with parameters attr, old and new.
  • Set the yr name to slider.value and set source.data = new_data.
  • Make a slider object called slider using the Slider() function with a start year of 1970, end year of 2010, step of 1, value of 1970, and title of 'Year'.
  • Attach the callback to the 'value' property of slider. This can be done using on_change() and passing in 'value' and update_plot.
  • Make a row layout of widgetbox(slider) and plot and add it to the current document.