## 1. Basic Plotting¶

This section covers the bokeh.plotting interface. The interface is a mid-level component, and the main idea can be described by the statement:

Starting from simple default figures (with sensible default tools, grids and axes), add markers and other shapes whose visual attributes are tied to directly data.

You'll will see that it's possible to customize and change all of the defaults, but having them means that it is possible to get up and running very quickly.

### Imports and Setup¶

# load ALL base resources will used later
#
from bokeh import events
from bokeh.io import output_notebook, show
from bokeh.plotting import figure

from bokeh.layouts import gridplot, column, row
from bokeh.models import Button, ColumnDataSource, CustomJS,  Div, HoverTool, TapTool
from bokeh.models.widgets import Slider, Button

from random import random
import numpy as np


When using the bokeh.plotting interface, there are a few common imports:

### Library version¶

To display the currently loaded version of Bokek in the Jupyter notebook, call output_notebook(). You need to call this once, and all subsequent calls to show() will display inline in the notebook. If everything is working correctly, the Bokeh logo and a message like BokehJS x.y.z successfully loaded (the version) is shown.

output_notebook()


### Sample data¶

The example notebooks may use Bokeh sample data. If you want to use Bokeh sample data with your notebooks as well, the data files can be downloaded by running the following Python code:

import bokeh.sampledata


(inside a cell) in your notebooks.

#nbi:hide_in
import bokeh.sampledata

#nbi:hide_out

Using data directory: C:\Users\jadams\.bokeh\data
Skipping 'CGM.csv' (checksum match)
Skipping 'US_Counties.zip' (checksum match)
Skipping 'us_cities.json' (checksum match)
Skipping 'unemployment09.csv' (checksum match)
Skipping 'AAPL.csv' (checksum match)
Skipping 'FB.csv' (checksum match)
Skipping 'GOOG.csv' (checksum match)
Skipping 'IBM.csv' (checksum match)
Skipping 'MSFT.csv' (checksum match)
Skipping 'WPP2012_SA_DB03_POPULATION_QUINQUENNIAL.zip' (checksum match)
Skipping 'gapminder_fertility.csv' (checksum match)
Skipping 'gapminder_population.csv' (checksum match)
Skipping 'gapminder_life_expectancy.csv' (checksum match)
Skipping 'gapminder_regions.csv' (checksum match)
Skipping 'world_cities.zip' (checksum match)
Skipping 'airports.json' (checksum match)
Skipping 'movies.db.zip' (checksum match)
Skipping 'airports.csv' (checksum match)
Skipping 'routes.csv' (checksum match)


### Scatter Plots¶

Bokeh can draw many types of visual shapes (called glyphs), including lines, bars, patches, hex tiles and more. One of the most common visualization tasks is to draw a scatter plot of data using small marker glyphs to represent each point.

In this section you will see how to use Bokeh's various marker glyphs to create simple scatter plots.

The basic outline is:

• create a blank figure: p = figure(...)
• call a glyph method such as p.circle on the figure
• show the figure

Execute the cell below to create a small scatter plot with circle glyphs:

# create a new plot using figure
p = figure(
plot_height=400,
plot_width=800,
)

# add a circle renderer with x and y coordinates, size, color, and alpha
p.circle(
[1, 2, 3, 4, 5], [6, 7, 2, 4, 5],
size=15, line_color="navy",
fill_color="orange",
fill_alpha=0.5
)

# show the results
show(p)


In the output above, you can see the effect of the different options for line_color, fill_alpha, etc. Try changing some of these values and re-executing the cell to update the plot. All Bokeh scatter markers accept size (measured in screen space units) as a property. Circles in particular also have radius (measured in "data" space units).

To scatter square markers instead of circles, you can use the square method on figures.

# create a new plot using figure
p = figure(
plot_height=400,
plot_width=800,
)

# add a square renderer with a size, color, alpha, and sizes
p.square(
[1, 2, 3, 4, 5], [6, 7, 2, 4, 5],
size=[10, 15, 20, 25, 30],
color="firebrick",
alpha=0.6
)

# show the results
show(p)


Note that in the example above, we are also specifying different sizes for each individual marker. In general, all of a glyph's properties can be "vectorized" in this fashion. Also note that we have passed color as a shorthand to set both the line and fill colors easily at the same time. This is a convenience specific to bokeh.plotting.

There are many marker types available in Bokeh, you can see details and example plots for all of them in the reference guide by clicking on entries in the list below:

### Line Plots¶

Another common visualization task is the drawing of line plots. This can be accomplished in Bokeh by calling the p.line(...) glyph method as shown below.

# create a new plot using figure (with a title)
p = figure(
plot_height=400,
plot_width=800,
title="My Line Plot"
)

p.line(
[1, 2, 3, 4, 5],
[6, 7, 2, 4, 5],
line_width=2
)

# show the results
show(p)


In addition to line_width, there are other options such as line_color or line_dash that can be set. Try setting some of the other properties of line and re-running the cell above.

#### Datetime axes¶

It's often the case that timeseries data is represented by drawing lines. Let's look at an example using the "glucose" data set, which is available in a Pandas dataframe:

from bokeh.sampledata.glucose import data

isig glucose
datetime
2010-03-24 09:51:00 22.59 258
2010-03-24 09:56:00 22.52 260
2010-03-24 10:01:00 22.23 258
2010-03-24 10:06:00 21.56 254
2010-03-24 10:11:00 20.79 246

We'd like to plot a subset of this data, and have a nice datetime axis as well. We can ask Bokeh for a datetime axis by passing x_axis_type="datetime" to the call to figure. This is shown below, as well as configuration of a some other options such as plot dimensions, axis titles, and grid line properies.

# reduce data size to one week
week = data.loc['2010-10-01':'2010-10-08']

p = figure(
plot_height=400,
plot_width=800,
x_axis_type="datetime",
title="Glocose Range"
)

p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

p.line(week.index, week.glucose)

# show the results
show(p)


#### Hex Tiling¶

Bokeh supports drawing low level hex tilings using axial coordinates and the hex_tile method, as described in the Hex Tiles section of the User's Guide. However, one of the most common uses of hex tilings is to visualize binning. Bokeh encapsulates this common operation in the hexbin function, whose output can be passed directly to hex_tile as seen below.

from bokeh.palettes import Viridis256
from bokeh.util.hex import hexbin

n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

bins = hexbin(x, y, 0.1)

# color map the bins by hand, will see how to use linear_cmap later
color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]

# match_aspect ensures neither dimension is squished, regardless of the plot size
p = figure(
plot_height=400,
plot_width=800,
tools="wheel_zoom,reset",
match_aspect=True,
background_fill_color='#440154'
)

p.grid.visible = False
p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)

# show the results
show(p)


### Images¶

Another common task is to display images, which might represent heat maps, or sensor data of some sort. Bokeh provides two glyph methods for displaying images:

• image which can be used, together with a palette, to show colormapped 2d data in a plot
• image_rgba which can be used to display raw RGBA pixel data in a plot.

The first example below shows how to call image with a 2d array and a palette

N = 500
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)
xx, yy = np.meshgrid(x, y)

img = np.sin(xx)*np.cos(yy)

p = figure(
plot_height=400,
plot_width=800,
x_range=(0, 10),
y_range=(0, 10)
)

# must give a vector of image data for image parameter
p.image(
image=[img],
x=0,
y=0,
dw=10,
dh=10,
palette="Spectral11"
)

# show the results
show(p)
{"ft_lang_label":"__label__en","ft_lang_prob":0.7490818,"math_prob":0.88381904,"size":8110,"snap":"2022-40-2023-06","text_gpt3_token_len":2159,"char_repetition_ratio":0.13113743,"word_repetition_ratio":0.05228216,"special_character_ratio":0.26794082,"punctuation_ratio":0.1705665,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9837423,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-06T12:24:27Z\",\"WARC-Record-ID\":\"<urn:uuid:d6b5b1b4-e5a9-497f-8d5e-53a1a4295a09>\",\"Content-Length\":\"1049005\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:094c049d-8adf-4aee-92db-869975d0efe9>\",\"WARC-Concurrent-To\":\"<urn:uuid:8e6caff3-0915-49b6-abfb-d974ca957842>\",\"WARC-IP-Address\":\"35.231.210.182\",\"WARC-Target-URI\":\"https://jekyll.one/pages/public/manuals/jupyter/notebooks/textbooks/j1_bokeh_01_basic_plotting.html\",\"WARC-Payload-Digest\":\"sha1:7JHK5LNXIFY6TFADTE2OAMJASXCIEEYI\",\"WARC-Block-Digest\":\"sha1:CRWHALDC4P6KRYL6J3YISXHSQTO4J2OP\",\"WARC-Truncated\":\"length\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500339.37_warc_CC-MAIN-20230206113934-20230206143934-00455.warc.gz\"}"}