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