How can Bokeh be used to visualize multiple bar plots in Python?

PythonServer Side ProgrammingProgramming

Bokeh is a Python package that helps in data visualization. It is an open source project. Bokeh renders its plot using HTML and JavaScript. This indicates that it is useful while working with web-

Bokeh converts the data source into a JSON file. This file is used as an input to BokehJS, which is a JavaScript library. This BokehJS is written in TypeScript that helps render visualization on modern browsers.

Dependencies of Bokeh

Numpy
Pillow
Jinja2
Packaging
Pyyaml
Six
Tornado
Python−dateutil

Installation of Bokeh on Windows command prompt

pip3 install bokeh

Installation of Bokeh on Anaconda prompt

conda install bokeh

Let us see an example −

Example

from bokeh.plotting import figure, output_file, show
from bokeh.transform import dodge
labs = ['label_1', 'label_2', 'label_3']
vals = ['val_1','val_2','val_3']
my_data = {'labs':labs,
   'val_1':[2,5,11],
   'val_2':[34,23,1],
   'val_3':[25, 34, 23]
}
fig = figure(x_range = labs, plot_width = 300, plot_height = 300)
fig.vbar(x = dodge('labs', -0.25, range = fig.x_range), top = 'val_1',
width = 0.2,source = my_data, color = "green")
fig.vbar(x = dodge('labs', 0.0, range = fig.x_range), top = 'val_2',
width = 0.2, source = my_data,color = "cyan")
fig.vbar(x = dodge('labs', 0.25, range = fig.x_range), top = 'val_3',
width = 0.2,source = my_data,color = "blue")
show(fig)

Output

Explanation

  • The required packages are imported, and aliased.

  • The figure function is called along with plot width and height.

  • The data is defined in lists.

  • The ‘output_file’ function is called to mention the name of the html file that will be generated.

  • The ‘vbar’ function present in Bokeh is called, along with data.

  • The ‘show’ function is used to display the plot.

raja
Published on 18-Jan-2021 12:44:37
Advertisements