So, today we will create a simple chart with a library called Bokeh. It is a very easy to use library.

First create a new project and point the environment to our honeyBadger env, (Check this post to see how to do this)

Call the project: myFirstBokeh.

Your solution explorer should look like this after changing the environment:

So, how do we create a chart? Well, let’s see this Bokeh library, they have some neat examples in there

https://bokeh.pydata.org/en/latest/docs/user_guide/quickstart.html#userguide-quickstart

They have this code in there

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# output to static HTML file
output_file("lines.html")

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

# show the results
show(p)

If we run this however, it won’t work! Also notice how it shows up in our Visual Studio:

This is Visual Studio saying “I have no idea what this is that you are asking me to import”.

Let’s analyze this for a second. We are asking Visual Studio to import:

  • figure
  • output_file
  • show

From something called bokeh.plotting

This means we are trying to import into our Python project those 3 functions of the bokeh.plotting module. These 3 do stuff that we need to produce a chart and the good thing is we only have to import them into our project and call them.

Anaconda Navigator

Ok but how do we make Visual Studio get that library. That’s where our Anaconda Navigator comes in, open it and click on our honeyBadger environment.

 

Once in here click on the dropdown (3) and change it to Not Installed and in the Search Packages box type Bokeh. You should see it coming up like this:

Now click on the checkbox (or right click on the name then Mark for Installation) and click Apply on the bottom right then Apply again on the popup

After it finishes, you can click on the dropdown again to see that it is there, also you can click on the “x” to remove the search and you will see all the modules installed in our honeyBadger environment.

All right, after we install Bokeh using Anaconda we can go back to Visual Studio and if we open the Python Environments window (Tools -> Python -> Python Environments) we will see that Visual Studio is working in integrating Bokeh in its Intellisense which basically means that when we type functions from that library, Visual Studio will open a little window (as we type) providing the parts that belong to the libraries we are using (you will see this soon)

You will also notice that in our Solution Explorer if we expand our honeyBadger environment we will have Bokeh in there.

All right, so now hit run and you might have a web browser opening for you with the chart you programmed.

That was easy, wasn’t it? You can even interact with the chart using the tools on the top right But let’s analyze the code now after the import statements.

Data to Plot

# prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

A chart needs data to plot (y) and another set of data for the horizontal axis (x)

Plot to HTML File


# output to static HTML file
output_file("lines.html")

Bokeh offers this amazing capability to create the chart to a file which you could then put in a web server.

A Figure


# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

A figure is the area where the plot (the lines) will be drawn, think of it as the canvas of a picture. This Canvas has certain elements like the Title and the labels.

Plots Within the Figure


# add a line renderer with legend and line thickness
p.line(x, y, legend="Temp.", line_width=2)

Here we add a line plot to our figure P. We also pass the data we want to plot (x,y) and define legend and line width

Render the Figure


# show the results
show(p)

Finally this line creates the chart and opens the HTML file.

Now that you know how to get Bokeh running, try modifying the charts and also run some of the examples in the Bokeh website. Have fun Bokehing!

Leave a Reply

Your email address will not be published. Required fields are marked *