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)
Este exercício faz parte do curso
Interactive Data Visualization with Bokeh
Instruções do exercício
- Import the
Selectwidget along withCustomJS. - Create the
Selectwidget, assigningoptionsas a list containing"Boeing","Delta", and"Southwest"(in that order), avalueof"Boeing", and atitleof"Airline". - Set up the widget to implement the
callbackfunction on a change in"value".
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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))