Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's familiarize ourselves with some Bokeh concepts, imports, and terminology. Then we can generate a basic plot and show what Bokeh generates for us.
-
0:00
Before we get to our data source,
-
0:02
let's familiarize ourselves with some concepts of the Bokeh Library.
-
0:05
Some of the common imports we'll use and some terminology in Bokeh.
-
0:09
We'll start by generating a basic plot with some Python lists.
-
0:13
Simulated showcase, our tools, and some options.
-
0:16
To get started, we need to have our libraries available.
-
0:19
I've included a requirements.txt file that has the libraries we'll be using for
-
0:23
this course.
-
0:24
Download and unzip that file, and
-
0:28
install the requirements by running the following
-
0:34
python3 -m pip install -r requirements.txt.
-
0:41
Once we have those libraries set up and installed,
-
0:44
we can start a new project file in Python.
-
0:53
Stage1_2.py.
-
0:58
Let's begin with our imports, and talk briefly about them.
-
1:02
From bokeh.io, import output file and show.
-
1:07
And from bokeh.plotting, And import figure.
-
1:16
Great, output file defines the name of our output file
-
1:21
to be generated when the show method.
-
1:24
When the show method is called, it will generate the HTML and
-
1:27
display it in a browser.
-
1:29
There are other possibilities for output, such as to a Jupiter notebook, but
-
1:33
that is beyond the scope of this course.
-
1:35
Jupiter notebooks are amazing though, and you should check the links
-
1:38
in the teacher's notes to learn more about those later.
-
1:41
The next import, figure, allows us to create figure objects for plotting and
-
1:47
has options for things such as plot width, height, tool selection, and many more.
-
1:52
But what is a figure object?
-
1:55
It is where our visualization will be drawn.
-
1:58
It is essentially Bokeh's built in graph paper if you will, and provides us
-
2:03
with some default access grids and tools that will simplify plot creation.
-
2:09
Let's define our output file and name it test.html.
-
2:19
Then, we'll set up a basic figure object and call it plot.
-
2:27
That is 600 pixels wide
-
2:32
600 pixels tall,
-
2:40
And includes a few of Bokeh's built-in tools for figures.
-
2:51
These tools will be used in the browser which we'll see and
-
2:55
talk about in a little bit.
-
2:57
Now, we need something to plot or visualize, right?
-
3:01
We can pass into our plot, a variety of data types.
-
3:05
Things like lists, arrays, and panda's data frames.
-
3:09
Really, anything that is a sequence can be passed in as x and y coordinates.
-
3:14
One thing to remember here is that the sequences must be of the same length.
-
3:17
If you pass in five values for x, you will need to pass five value for y.
-
3:23
We also have several options for the shape of our plot point or glyph.
-
3:26
Let's plot some points with a square shape of size 20.
-
3:34
Let's choose square for x values,
-
3:38
lets do (x=[1, 2, 4, 8, 10]).
-
3:43
Our y values, Can be ([6,
-
3:49
2, 18, 4, 9]).
-
3:55
And we want them to have a size, 20.
-
3:57
These are a sequence of x and y pairs.
-
4:00
So the first point will be 1, 6, the second, 2, 2 and so on.
-
4:05
Now, we just need to pass our plot into the show method and call it, like so.
-
4:12
Then we can run our script.
-
4:20
And there it is, automatically in the browser.
-
4:23
Now, depending on your version of your operating system, you may see an execution
-
4:28
error about not being able to understand the open location message.
-
4:33
Don't worry about this if you see it.
-
4:35
So long as the plot loads correctly, your code works.
-
4:38
You will notice, off on the upper right-hand side,
-
4:40
the tools we requested when configuring our plot, Pan, Box Zoom, and Reset.
-
4:48
Pan allows us to move the plot around.
-
4:52
Box Zoom allows us to zoom in on a specific location in the plot.
-
4:57
And Reset, well, it resets the plot.
-
5:00
Try them out on your own to get a feel for them.
-
5:03
I put a link in the teacher's notes for other tools you can include as well.
-
5:07
Now, if we go back and take a look at what was generated by Bokeh,
-
5:12
in our test.html output file, and let Python do a bit of code reformatting
-
5:18
with Control+Alt+L on Windows or Alt+Command+L on Mac.
-
5:25
We see that it generates a standard HTML file with calls to Bokeh's CSS style sheet
-
5:30
and JavaScript on lines seven and nine, respectively.
-
5:36
If we scroll down to around line 36, we see
-
5:40
that Bokeh generated a JSON document which is used to describe our visualization.
-
5:46
We won't spend a lot of time going over this, but I wanted to point out that this
-
5:50
JSON object includes all of the configuration for our plot.
-
5:54
For example, if we look at around line 222,
-
6:00
We'll see our column names and the associated data that we provided.
-
6:03
And if we look at around line 145, We see that our data is of type ColumnDataSource.
-
6:12
As you can see, having Bokeh generate all of this for
-
6:15
us to built our plot is a huge time saver and all done on our end in native Python.
-
6:22
Let's take a short break before we start working with more complex data structures
-
6:26
then basic lists and introduce a powerful Bokeh data object, ColumnDataSource.
You need to sign up for Treehouse in order to download course files.
Sign up