Bokeh package can be installed here.


In this intro we will create a line chart.



To Start.


Create a folder named line chart. Inside save a python script named linechart.py.

We will be running the file in terminal. I use Mac so command lines might differ from Windows. If you have Mac or Linux, shell commands are similar.

Open the linechart.py script


Import the modules

from bokeh.plotting import figure, output_file, show

-figure is used for labeling, tools, title and data types.
-Here output_file is used to create an standalone html.
-show will open the results in the browser.


Prepare sample data for the chart:

x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0]

Name the output file:

output_file("line_chart.html")

Plot the figure:

p = figure(
   tools="pan,box_zoom,reset,save",
   title="Sample line chart",
   x_axis_label='X', y_axis_label='Y')

Tools are used to create interaction with the chart.


Create a sample line and show the results:

p.line(x, y, legend="Blue line", line_color="blue", line_width=2)
show(p)

One line complete script: linechart.py

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0]

# output stand alone HTML file
output_file("line_chart.html")

# plot
p = figure(
   tools="pan,box_zoom,reset,save",
   title="Sample line chart",
   x_axis_label='X', y_axis_label='Y'
)
# Sample line
p.line(x, y, legend="Blue line", line_color="blue", line_width=2)

show(p)

Navigate to the line chart folder in terminal (command line) and run the script

python linechart.py

Running this command will create a stand alone html in the folder and open results in a browser.




More Lines

We can add some more lines by preparing some more data.

y0 = [i**2 for i in x]
y1 = [i**3 for i in x]
y2 = [i+2 for i in y]

Add lines and circles for data points.

p.line(x, y0, legend="Red line", line_color="red", line_width=2)
p.circle(x, y0, fill_color="white", size=8)

p.line(x, y1, legend="Green line", line_color="Green")
p.circle(x, y1, fill_color="white",  size=6)

p.line(x, y2, legend="Black line", line_color="black", line_dash="3 3")

The code should look like this:

from bokeh.plotting import figure, output_file, show

# prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0]
y0 = [i**2 for i in x]
y1 = [i**3 for i in x]
y2 = [i+2 for i in y]

# output stand alone HTML file
output_file("line_chart.html")

# plot
p = figure(
   tools="pan,box_zoom,reset,save",
   title="Sample line chart",
   x_axis_label='X', y_axis_label='Y'
)

p.line(x, y, legend="Blue line", line_color="blue", line_width=2)

p.line(x, y0, legend="Red line", line_color="red", line_width=2)
p.circle(x, y0, fill_color="white", size=8)

p.line(x, y1, legend="Green line", line_color="Green")
p.circle(x, y1, fill_color="white",  size=6)

p.line(x, y2, legend="Black line", line_color="black", line_dash="3 3")

# show the results
show(p)

The results after running the second script: