Bokeh Basics II

3 minute read

Subplots

To create subplots we would need to import gridplot from bokeh.layouts.


# Load the car dataset from vega
df_cars = vds.cars()
df_cars.head()

from bokeh.layouts import gridplot

# load data from the cars dataset

subplot_x1 = df_cars['Acceleration']; subplot_y1 = df_cars['Miles_per_Gallon']
subplot_x2 = df_cars['Cylinders']; subplot_y2 = df_cars['Miles_per_Gallon']
subplot_x3 = df_cars['Horsepower']; subplot_y3 = df_cars['Miles_per_Gallon']
subplot_x4 = df_cars['Weight_in_lbs']; subplot_y4 = df_cars['Miles_per_Gallon']


scatter_plot = figure(title='Weight and MPH Comparison',plot_height= 400, plot_width=600, 
                      x_axis_label='Weight in pounds', y_axis_label='MPH')
scatter_plot.circle(x_weight, y_mph, size=10, line_color='gray', fill_color='blue', fill_alpha=0.5)

# create an instance for every subplot
subplot1 = figure(title='Acceleration & MPH Comparison', plot_width = 300, plot_height=300, x_axis_label='Acceleration', y_axis_label='MPH')
subplot2 = figure(title='Cylinders & MPH Comparison', plot_width = 300, plot_height=300, x_axis_label='Cylinders', y_axis_label='MPH')
subplot3 = figure(title='Horsepower & MPH Comparison', plot_width = 300, plot_height=300, x_axis_label='Horsepower', y_axis_label='MPH')
subplot4 = figure(title='Weight/lbs & MPH Comparison', plot_width = 300, plot_height=300, x_axis_label='Weight / lbs', y_axis_label='MPH')

# create the plots
subplot1.circle(subplot_x1, subplot_y1, color='red')
subplot2.circle(subplot_x2, subplot_y2, color='blue')
subplot3.circle(subplot_x3, subplot_y3, color='green')
subplot4.circle(subplot_x4, subplot_y4, color='gray')

# create the grid
grid = gridplot([subplot1, subplot2, subplot3, subplot4], ncols=2)

output_file('/subplots_grid.html')
# show grid
show(grid)

Bokeh Plot

Linked Plots

In Bokeh, we can link multiple plots so they can be manipulated as one. We would reuse the same grid plot above and link the first three plots to show this concept.

from bokeh.layouts import gridplot

# load data from the cars dataset

linked_plotx1 = df_cars['Acceleration']; linked_ploty1 = df_cars['Miles_per_Gallon']
linked_plotx2 = df_cars['Cylinders']; linked_ploty2 = df_cars['Miles_per_Gallon']
linked_plotx3 = df_cars['Horsepower']; linked_ploty3 = df_cars['Miles_per_Gallon']
linked_plotx4 = df_cars['Weight_in_lbs']; linked_ploty4 = df_cars['Miles_per_Gallon']


# create an instance for every subplot
linked_plot1 = figure(title='Acceleration & MPH Comparison', plot_width = 300, plot_height=300, 
                      x_axis_label='Acceleration', y_axis_label='MPH')
linked_plot2 = figure(title='Cylinders & MPH Comparison', plot_width = 300, plot_height=300, 
                      x_axis_label='Cylinders', y_axis_label='MPH', x_range=linked_plot1.x_range, y_range=linked_plot1.y_range)
linked_plot3 = figure(title='Horsepower & MPH Comparison', plot_width = 300, plot_height=300, 
                      x_axis_label='Horsepower', y_axis_label='MPH', x_range=linked_plot1.x_range, y_range=linked_plot1.y_range)
linked_plot4 = figure(title='Weight/lbs & MPH Comparison', plot_width = 300, plot_height=300, 
                      x_axis_label='Weight / lbs', y_axis_label='MPH')

# create the plots
linked_plot1.circle(linked_plotx1, linked_ploty1, color='red')
linked_plot2.circle(linked_plotx2, linked_ploty2, color='blue')
linked_plot3.circle(linked_plotx3, linked_ploty3, color='green')
linked_plot4.circle(linked_plotx4, linked_ploty4, color='gray')

# create the grid
linked_grid_plot = gridplot([linked_plot1, linked_plot2, linked_plot3, linked_plot4], ncols=2)

output_file('/linked_grid_plot.html')
# show grid
show(linked_grid_plot)
Bokeh Plot

Adding Lasso and Box Select to Linked Plots

We can explore our data further by adding a Lasso and Box select tools to our linked plots. For this example, we will work with a weather dataset which contains temperature readings as well as the weather conditions.

# load Seattle weather data from vega

df_weather = vds.seattle_weather()
df_weather.head()

date precipitation temp_max temp_min wind weather
0 2012-01-01 0.0 12.8 5.0 4.7 drizzle
1 2012-01-02 10.9 10.6 2.8 4.5 rain
2 2012-01-03 0.8 11.7 7.2 2.3 rain
3 2012-01-04 20.3 12.2 5.6 4.7 rain
4 2012-01-05 1.3 8.9 2.8 6.1 rain
from bokeh.transform import factor_cmap, factor_mark

TOOLS = 'box_select, lasso_select, reset, wheel_zoom, pan' 

weather_conditions = ['drizzle', 'rain', 'sun', 'snow', 'fog']
weather_markers = ['hex', 'cross', 'triangle', 'square', 'circle_x']

# create a ColumnDataSource

cds_weather = ColumnDataSource(df_weather)

# create 1st scatter plot
weather_plot = figure(plot_width=900, plot_height=400, x_axis_type='datetime',
                      y_axis_label='Temperature', tools=TOOLS)
weather_plot.circle('date', 'temp_max', size=10, fill_alpha=0.2, source=cds_weather)
# create 2nd scatter plot for the detailed weather data
weather_detail = figure(plot_width=900, plot_height=400, x_axis_type='datetime',
                      y_axis_label='Weather Conditions', tools=TOOLS)
weather_detail.scatter('date', 'temp_max', size=10, fill_alpha=0.2, source=cds_weather,
                     color=factor_cmap(field_name='weather', palette='Dark2_5',
                     factors=weather_conditions),
                     marker=factor_mark('weather', weather_markers, weather_conditions),
                     legend_group='weather')
# Configuring tooltips for the Hovertool
weather_plot.add_tools(HoverTool(tooltips=[('date', '@date{"%F"}'), ('temp', '@temp_max')],
                      formatters={'@date': 'datetime'}))
weather_detail.add_tools(HoverTool(tooltips=[('date', '@date{"%F"}'), ('condition', '@weather')],
                                   formatters={'@date': 'datetime'})))

# configure legend

weather_detail.legend.location='top_left'
weather_detail.legend.orientation='horizontal'


#create grid
weather_grid = gridplot([[weather_plot], [weather_detail]])
show(weather_grid)
output_file('/linked_lasso_box_grid_plot.html')                                   
Bokeh Plot

Creating a Color Bar Graph

Color Bars can quickly help us understand our data as the color palette changes based on the magnitude/intensity of the values. We can reuse the car dataset to illustrate this concept by graphing the relationship between MPG/Horsepoer and the car’s weight.

# display df_car's head
df_cars.head()
Name Miles_per_Gallon Cylinders Displacement Horsepower Weight_in_lbs Acceleration Year Origin
0 chevrolet chevelle malibu 18.0 8 307.0 130.0 3504 12.0 1970-01-01 USA
1 buick skylark 320 15.0 8 350.0 165.0 3693 11.5 1970-01-01 USA
2 plymouth satellite 18.0 8 318.0 150.0 3436 11.0 1970-01-01 USA
3 amc rebel sst 16.0 8 304.0 150.0 3433 12.0 1970-01-01 USA
4 ford torino 17.0 8 302.0 140.0 3449 10.5 1970-01-01 USA
from bokeh.models import LinearColorMapper, ColorBar
from bokeh.transform import transform

# map values to a color pallette
color_mapper = LinearColorMapper(palette="Cividis256", low=df_cars.Weight_in_lbs.min(), 
                                 high=df_cars.Weight_in_lbs.max())

# create instance of the plot

color_bar_plot = figure(plot_width=800, plot_height=600,
                        x_axis_label="Horsepower", 
                        y_axis_label="MPG")

color_bar_plot.circle(x="Horsepower",
                      y="Miles_per_Gallon",
                      source=df_cars,
                      color=transform('Weight_in_lbs', color_mapper),
                      size=10,
                      alpha=0.5)

# add Hovertool 
color_bar_plot.add_tools(HoverTool(tooltips=[('MPG', '@Miles_per_Gallon'), ('Horsepower', '@Horsepower')]))


# display the colors in the bar based on the color mapper
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=10, 
                     location=(0, 0), title="Weight")
color_bar_plot.add_layout(color_bar, 'right')

show(color_bar_plot)
output_file('/color_bar_plot.html')
Bokeh Plot