Python Bokeh tutorial – Interactive Data Visualization with Bokeh
Features of Bokeh:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
- Flexibility: Bokeh can be used for common plotting requirements and for custom and complex use-cases.
- Productivity: Its interaction with other popular Pydata tools (such as Pandas and Jupyter notebook) is very easy.
- Interactivity: It creates interactive plots that change with the user interaction.
- Shareable: Visual data are shareable. They can also be rendered in Jupyter notebooks.
- Open source: Bokeh is an open-source project.
This tutorial aims at providing insight to Bokeh using well-explained concepts and examples with the help of a huge dataset. So let’s dive deep into the Bokeh and learn all it from basic to advance.
Table Of Content
- Bokeh Interfaces – Basic Concepts of Bokeh
- Getting Started
- Annotations and Legends
- Plotting Different Types of Plots
- Creating Different Shapes
- Plotting Multiple Plots
- Interactive Data Visualization
- Creating Different Types of Glyphs
- Visualizing Different Types of Data
- More Topics on Bokeh
Bokeh is supported by CPython 3.6 and older with both standard distribution and anaconda distribution. Bokeh package has the following dependencies.
1. Required Dependencies
- typing_extensions >=3.7.4
2. Optional Dependencies
- Selenium, GeckoDriver, Firefox
Bokeh can be installed using both conda package manager and pip. To install it using conda type the below command in the terminal.
conda install bokeh
This will install all the dependencies. If all the dependencies are installed then you can install the bokeh from PyPI using pip. Type the below command in the terminal.
pip install bokeh
Refer to the below article to get detailed information about the installation of Bokeh.
Bokeh Interfaces – Basic Concepts of Bokeh
Bokeh is simple to use as it provides a simple interface to the data scientists who do not want to be distracted by its implementation and also provides a detailed interface to developers and software engineers who may want more control over the Bokeh to create more sophisticated features. To do this Bokeh follows the layered approach.
This is the mid-level interface that provides Matplotlib or MATLAB like features for plotting. It deals with the data that is to be plotted and creating the valid axes, grids, and tools. The main class of this interface is the Figure class.
After the installation and learning about the basic concepts of Bokeh let’s create a simple plot.
In the above example, we have created a simple Plot with the Title as Bokeh Line Graph. If you are using Jupyter then the output will be created in a new tab in the browser.
Annotations and Legends
Annotations are the supplemental information such as titles, legends, arrows, etc that can be added to the graphs. In the above example, we have already seen how to add the titles to the graph. In this section, we will see about the legends.
Adding legends to your figures can help to properly describe and define them. Hence, giving more clarity. Legends in Bokeh are simple to implement. They can be basic, automatically grouped, manually mentioned, explicitly indexed, and also interactive.
In the above example, we have plotted two different lines with a legend that simply states that which is line 1 and which is line 2. The color in the legends is also differentiated by the color.
Refer to the below articles to get detailed information about the annotations and legends
Legends in Bokeh can be customized using the following properties.
Property Description legend.label_text_font change default label font to specified font name legend.label_text_font_size font size in points legend.location set the label at specified location. legend.title set title for legend label legend.orientation set to horizontal (default) or vertical legend.clicking_policy specify what should happen when legend is clicked
Plotting Different Types of Plots
Glyphs in Bokeh terminology means the basic building blocks of the Bokeh plots such as lines, rectangles, squares, etc. Bokeh plots are created using the bokeh.plotting interface which uses a default set of tools and styles.
Line charts are used to represent the relation between two data X and Y on a different axis. A line plot can be created using the line() method of the plotting module.
Refer to the below articles to get detailed information about the line plots.
Bar plot or Bar chart is a graph that represents the category of data with rectangular bars with lengths and heights that is proportional to the values which they represent. It can be of two types horizontal bars and vertical bars. Each can be created using the hbar() and vbar() functions of the plotting interface respectively.
Example 1: Creating horizontal bars.
Example 2: Creating the vertical bars
Refer to the below articles to get detailed information about the bar charts.
A scatter plot is a set of dotted points to represent individual pieces of data in the horizontal and vertical axis. A graph in which the values of two variables are plotted along X-axis and Y-axis, the pattern of the resulting points reveals a correlation between them. It can be plotted using the scatter() method of the plotting module.
Refer to the below articles to get detailed information about the scatter plots.
Patch Plot shades a region of area to show a group having same properties. It can ve created using the patch() method of the plotting module.
Refer to the below articles to get detailed information about the Patch Plot.
Area plots are defined as the filled regions between two series that share a common areas. Bokeh Figure class has two methods which are – varea(), harea()
varea(x, y1, y2, **kwargs)
harea(x1, x2, y, **kwargs)
Example 1: Creating vertical area plot
Example 2: Creating horizontal area plot
Refer to the below articles to get detailed information about the area charts
Bokeh Does not provide a direct method to plot the Pie Chart. It can be created using the wedge() method. In the wedge() function, the primary parameters are the x and y coordinates of the wedge, the radius, the start_angle and the end_angle of the wedge. In order to plot the wedges in such a way that they look like a pie chart, the x, y, and radius parameters of all the wedges will be the same. We will only adjust the start_angle and the end_angle.
Refer to the below articles to get detailed information about the pie charts.
Creating Different Shapes
The Figure class in Bokeh allows us create vectorised glyphs of different shapes such as circle, rectangle, oval, polygon, etc. Let’s discuss them in detail.
Bokeh Figure class following methods to draw circle glyphs which are given below:
- circle() method is a used to add a circle glyph to the figure and needs x and y coordinates of its center.
- circle_cross() method is a used to add a circle glyph with a ‘+’ cross through the center to the figure and needs x and y coordinates of its center.
- circle_x() method is a used to add a circle glyph with a ‘X’ cross through the center. to the figure and needs x and y coordinates of its center.
Refer to the below articles to get detailed information about the circle glyphs
oval() method can be used to plot ovals on the graph.
Refer o the below articles to get detailed information about the oval glyphs.
Triangle can be created using the triangle() method.
Refer to the below article to get detailed information about the triangles.
Just like circles and ovals rectangle can also be plotted in Bokeh. It can be plotted using the rect() method.
Bokeh can also be used to plot multiple polygons on a graph. Plotting multiple polygons on a graph can be done using the multi_polygons() method of the plotting module.
Refer to the below articles to get detailed information about the polygon glyphs.
Plotting Multiple Plots
There are several layouts provided by the Bokeh in order to creat Multiple Plots. These layouts are:
- Vertical Layout
- Horizontal Layout
- Grid Layout
Vertical Layout set all the plots in the vertical fashion and can be created uising the column() method.
Horizontal Layout set all the plots in the horizontal fashion. It can be created using the row() method.
gridplot() method can be used to arrange all the plots in the grid fashion. we can also pass None to leave a space empty for a plot.
Interactive Data Visualization
One of the key feature of Bokeh which differentiate it from other visualizing libraries is adding interaction to the Plot. Let’s see various interactions that can be added to the plot.
Configuring Plot Tools
In all the above graphs you must have noticed a toolbar that appears mostly at the right of the plot. Bokeh provides us the methods to handle these tools. Tools can be classified into four categories.
- Gestures: These tools handle the gestures such as pan movement. There are three types of gestures:
- Pan/Drag Tools
- Click/Tap Tools
- Scroll/Pinch Tools
- Actions: These tools handle when a button is pressed.
- Inspectors: These tools report information or annotate the graph such as HoverTool.
- Edit Tools: These are multi gestures tools that can add, delete glyphs from the graph.
Adjusting the Position of the ToolBar
We can specify the position of the toolbar according to our own needs. It can be done by passing the toolbar_location parameter to the figure() method. The possible value to this parameter is –
In the section annotations and legends we have seen the list of all the parameters of the legends, however, we have not discussed the click_policy parameter yet. This property makes the legend interactive. There are two types of interactivity –
- Hiding: Hides the Glyphs.
- Muting: Hiding the glyph makes it vanish completely, on the other hand, muting the glyph just de-emphasizes the glyph based on the parameters.
Example 1: Hiding the legend
Example 2: Muting the legend
Adding Widgets to the Plot
Bokeh provides GUI features similar to HTML forms like buttons, slider, checkbox, etc. These provide an interactive interface to the plot that allows to change the parameters of the plot, modifying plot data, etc. Let’s see how to use and add some commonly used widgets.
Button(label, icon, callback)
Slider(start, end, step, value)
- Tab Widget: Tab Widget adds tabs and each tab show a different plot.
Creating Different Types of Glyphs
- Diamond Glyph
- Dash Glyph
- Cross Glyph
- Diamond Glyph
- Diamond Cross Glyph
- Diamond Dot Glyph
- Ray Glyph
- Triangle Pins Glyph
- Triangle with Dots Glyph
- Inverted Triangle Glyph
- Plus Glyph
- Quadrilateral Glyph
- Y Glyph
- X Glyph
- Oval Glyph
- Hexagon Glyph
- Hexagon Tiles Glyph
- Hexagon Dots Glyph
- Square with Cross Glyph
- Square with Dots Glyph
- Square with X’s Glyph
- Ellipse glyph
- Annulus Glyph
- Bezier Glyph
- Asterisk Glyph
- AnnularWedge Glyph
- Step Glyph
Visualizing Different Types of Data
- Python Bokeh – Visualizing the Iris Dataset
- Python Bokeh – Visualizing Stock Data
- Interactive visualization of data using Bokeh
More Topics on Bokeh
- Python Bokeh – Colors Class
- How to use Color Palettes in Python-Bokeh?
- Python Bokeh – Plotting Quadratic Curves on a Graph
- Python Bokeh – Plotting Line Segments on a Graph
- Python Bokeh – Plotting Multiple Lines on a Graph
- Python Bokeh – Plotting glyphs over a Google Map
- Python Bokeh – Plot for all Types of Google Maps ( roadmap, satellite, hybrid, terrain)