Hello , You might be aware of D3 library written in Javascript for creating Graphs and other visualization of data. We also have a similar library in Python know as Bokeh. We can use to represent our data in many different ways. In this note we will be learning about bokeh.plotting . First you have to install bokeh on your machine. For basic usage have these dependencies installed:
But I will recommend you to use Anaconda Python Distribution . It includes robust versions of popular libraries for the Python scientific and data analysis stacks. After installing run the following command:
conda install bokeh
Alternatively, If you want to use bokeh Django or Flask then you can install it using PIP,
pip install bokeh
Now we have installed all necessary the dependencies, let's write our first program. In the following program we will plot a function f(x) which will change with the value of x. We will specify the Domain of the function and the function itself. Rest will be done by Bokeh.
#Import library
from bokeh.plotting import figure, output_file, show
#Specify Domain and Function
x = range(10)
y = [ a**0.5 for a in x]
#Graph will be written in the following file
output_file("bokeh_plot.html")
#Generate graph
plot = figure(title= "Y = X**0.5",
x_axis_label= 'X-Axis',
y_axis_label= 'Y-Axis')
plot.line(x, y, legend= 'f(X)', line_width = 2)
show(plot)
When you execute this script, bokeh_plot.html file will open in your Web Browser showing the resultant graph.
Understanding the Code
plot = figure(title= "Y = X**0.5",
x_axis_label= 'X-Axis',
y_axis_label= 'Y-Axis')
plot.line(x, y, legend= 'f(X)', line_width = 2)
The first line of the code written above create a blank graph with Graph Title and Axis labels. Second line plots a line using the 2 input array we gave to the method.
Basically the method in second line defines the type of marker which it will use in plotting the graph. There are lots of markers like circle, square, diamond, cross etc which can be used to mark points on graph. We can also plot multiple lines on one graph using multi_line method. Consider the following code:
#Import Library
from bokeh.plotting import figure, output_file, show
#Domain and Functions
x = list(range(10)) #You will get Type Error in Python 3 if you don't use list() function here.
y = [ a**0.5 for a in x]
y1 = [ a**2 for a in x]
#Output file in the current directory
output_file("bokeh_plot.html")
#Generate graph
plot = figure(title= "Y = f(x)",
x_axis_label= 'X-Axis',
y_axis_label= 'Y-Axis')
plot.multi_line(xs=[x, y] , ys=[x, y1], color=['red','green'])
show(plot)
The result of the above script will be like this:
Thats it for now. To learn more about different method of figure class, head to official docs of bokeh.plotting here.
In the next Chapter we will learn how we can use bokeh with Django and prepare an UI for generating graphs. Where user will input an Polynomial Equation and domain of function and will get a resultant graph.
Next Chapter : Use Bokeh Graphs in Django Template
Digital Artist Prateek | Visit My Website