Skip to content

Instantly share code, notes, and snippets.

@misho-kr
Last active July 22, 2021 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save misho-kr/119de719386da71fde345493c88063ec to your computer and use it in GitHub Desktop.
Save misho-kr/119de719386da71fde345493c88063ec to your computer and use it in GitHub Desktop.
Summary of "Interactive Data Visualization with Bokeh" course on Datacamp

Lead by Team Anaconda, Data Science Training

Bokeh is an interactive data visualization library for Python—and other languages—that targets modern web browsers for presentation. It can create versatile, data-driven graphics and connect the full power of the entire Python data science stack to create rich, interactive visualizations.

  • Glyphs
    • Visual shapes
      • circles, squares, triangles
      • rectangles, lines, wedges
    • With properties a ached to data
      • coordinates (x, y)
      • size, color, transparency
  • Markers
    • circle(), asterisk(), inverted_triangle(), x(), ...
  • Additional glyphs
    • Lines
    • Lines and Markers together
  • Patches
    • Useful for showing geographic regions
    • Data given as "list of lists"
  • patch(), patches(), annular_wedge(), image_url(), bezier(), etc.
from bokeh.io import output_file, show
from bokeh.plotting import figure

plot = figure(plot_width=400, tools='pan,box_zoom')
plot.circle([1,2,3,4,5], [8,6,5,2,3])

output_file('circle.html')
show(plot)

plot = figure()
plot.circle(x=10, y=[2,5,8,12], size=[10,20,30,40])

x = [1,2,3,4,5]
y = [8,6,4,2,3]

plot = figure()
plot.line(x, y, line_width=3)

output_file('line.html')
show(plot)

plot.line(x, y, line_width=2)
plot.circle(x, y, fill_color='white', size=10)

output_file('line.html')
show(plot)

xs = [[1,1,2,2], [2,2,4], [2,2,3,3]]
ys = [[2,5,5,2], [3,5,5], [2,3,4,2]]

plot = figure()
plot.patches(xs, ys,
      fill_color=['red', 'blue', 'green'],
      line_color='white')

output_file('patches.html')
show(plot)
  • Data formats
    • Python Basic Types
    • NumPy Arrays
    • Pandas
    • Column Data Source
      • Common fundamental data structure for Bokeh
      • Maps string column names to sequences of data
      • Often created automatically for you
      • Can be shared between glpyhs to link selections
      • Extra columns can be used with hover tooltips
from bokeh.io import output_file, show
from bokeh.plotting import figure
import numpy as np

x = np.linspace(0, 10, 1000)
y = np.sin(x) + np.random.random(1000) * 0.2

plot = figure()
plot.line(x, y)

output_file('numpy.html')
show(plot)

# Flowers is a Pandas DataFrame
from bokeh.sampledata.iris import flowers
plot = figure()
plot.circle(flowers['petal_length'],
            flowers['sepal_length'],
            size=10)

output_file('pandas.html')
show(plot)

from bokeh.models import ColumnDataSource
source = ColumnDataSource(data={
    'x':[1, 2, 3, 4, 5],
    'y':[8, 6, 5, 2, 3]})

from bokeh.sampledata.iris import flowers as df
source = ColumnDataSource(df)
  • Customizing glyphs
    • Selection appearance
    • Hover appearance
    • Color mapping
plot = figure(tools='box_select, lasso_select')
plot.circle(petal_length, sepal_length,
            selection_color='red',
            nonselection_fill_alpha=0.2,
            nonselection_fill_color='grey')

from bokeh.models import HoverTool

hover = HoverTool(tooltips=None, mode='hline')
plot = figure(tools=[hover, 'crosshair'])

# x and y are lists of random points
plot.circle(x, y, size=15, hover_color='red')

from bokeh.models import CategoricalColorMapper

mapper = CategoricalColorMapper(
              factors=['setosa', 'virginica', 'versicolor'],
              palette=['red', 'green', 'blue'])

plot = figure(x_axis_label='petal_length', axis_label='sepal_length')
plot.circle('petal_length', 'sepal_length',
              sze=10, source=source,
              color={'field': 'species', 'transform': mapper})

Basic plotting with Bokeh

Introduction to basic plotting with Bokeh. First plots, different data formats Bokeh understands, and visual customizations for selections and mouse hovering.

Layouts, Interactions, and Annotations

Combine multiple Bokeh plots into different kinds of layouts on a page, easily link different plots together, and add annotations such as legends and hover tooltips.

Building interactive apps with Bokeh

Bokeh server applications allow to connect all of the powerful Python libraries for data science and analytics, such as NumPy and pandas to create rich, interactive Bokeh visualizations. Bokeh's built-in widgets, add them to Bokeh documents alongside plots, and connect everything to real Python code using the Bokeh server.

Putting It All Together! A Case Study

Build a more sophisticated Bokeh data exploration application from the ground up based on the famous Gapminder dataset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment