Travel analysis
The hedge fund is aiming for the sky, and would like to understand the performance of airline stocks.
You will produce a line plot of stock price versus date, allowing viewers to switch between Delta Air Lines, Southwest Airlines, and Boeing. The figure and glyphs have been preloaded for you:
boeing = stocks.loc[stocks["name"] == "BA"]
delta = stocks.loc[stocks["name"] == "DAL"]
southwest = stocks.loc[stocks["name"] == "LUV"]
fig = figure(x_axis_label="Date", y_axis_label="Stock Price",
x_axis_type="datetime")
boeing_line = fig.line(x=boeing["date"], y=boeing["close"],
alpha=0.5)
delta_line = fig.line(x=delta["date"], y=delta["close"],
color="red", alpha=0.5)
sw_line = fig.line(x=southwest["date"], y=southwest["close"],
color="green", alpha=0.5)
This exercise is part of the course
Interactive Data Visualization with Bokeh
Exercise instructions
- Import the
Select
widget along withCustomJS
. - Create the
Select
widget, assigningoptions
as a list containing"Boeing"
,"Delta"
, and"Southwest"
(in that order), avalue
of"Boeing"
, and atitle
of"Airline"
. - Set up the widget to implement the
callback
function on a change in"value"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import modules
____
# Create Select widget
menu = ____(____=["____", "____", "____"], ____="____", ____="____")
callback = CustomJS(args=dict(line_1=boeing_line, line_2=delta_line,
line_3=sw_line), code="""
line_1.visible = true
line_2.visible = true
line_3.visible = true
if (this.value == "Boeing") {line_2.visible = false
line_3.visible = false}
else {line_1.visible = false}
if (this.value == "Delta") {line_1.visible = false
line_3.visible = false}
else {line_2.visible = false}
if (this.value == "Southwest") {line_1.visible = false
line_2.visible = false}
else {line_3.visible = false}
""")
# Set up interaction
menu.____("____", ____)
output_file(filename="airline_stocks.html")
show(column(menu, fig))