Reader small image

You're reading from  Hands-On Data Visualization with Bokeh

Product typeBook
Published inJun 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789135404
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Kevin Jolly
Kevin Jolly
author image
Kevin Jolly

Kevin Jolly is a formally educated data scientist with a master's degree in data science from the prestigious King's College London. Kevin works as a statistical analyst with a digital healthcare start-up, Connido Limited, in London, where he is primarily involved in leading the data science projects that the company undertakes. He has built machine learning pipelines for small and big data, with a focus on scaling such pipelines into production for the products that the company has built. Kevin is also the author of a book titled Hands-On Data Visualization with Bokeh, published by Packt. He is the editor-in-chief of Linear, a weekly online publication on data science software and products.
Read more about Kevin Jolly

Right arrow

Using Annotations, Widgets, and Visual Attributes for Visual Enhancement

Now that you have learned how to create plots and layouts in Bokeh, it is time to enhance them visually and add a layer of interactivity using annotations, widgets, and visual attributes.

Annotations are used to add supplemental information to your plots, such as titles, legends, and color maps that provide information about what the plot is trying to convey to the person who views your plot.

Widgets offer interactivity through buttons, drop-down menus, sliders, and textboxes. These widgets allow the person viewing the plot to interact with the plot and make changes to the way he or she wants to view it.

Visual attributes provide a vast range of visual enhancements to the plot, such as colors and fills for the lines and text, and interactivity enhancements such as the hover tool to hover over and select...

Technical requirements

Creating annotations to convey supplemental information

When creating plots it's fundamental to get across the story that the information in the plot is trying to convey. This can be done by adding titles, legends, and color maps to your plot.

Adding titles to plots

Titles are used to tell the reader about the overall story of the plot.

For the purposes of this chapter, we will use the S&P 500 stock data found on Kaggle. (https://www.kaggle.com/camnugent/sandp500/data).

We will also filter the data to just information about Apple stocks, as illustrated in the following code:

#Import the required packages

import pandas as pd

#Read in the data

df = pd.read_csv('all_stocks_5yr.csv')

#Convert the date column into...

Creating widgets to add interactivity to plots

One of Bokeh's most unique features is the ability to add widgets that add interactivity to plots. Widgets allow the user of the plot to change what they want to see by making selections, clicking on buttons, and typing into textboxes. In this section, you will learn about all the widgets that Bokeh can add to your Toolbelt.

The two imports that you will need to create and output any kind of widget are given here:

from bokeh.io import output_file, show
from bokeh.layouts import widgetbox

Creating a button widget

Buttons allow a user to click and make a selection. We can create a button widget in Bokeh by using the code shown here:

#Import the required packages

from bokeh.models...

Creating visual attributes to enhance style and interactivity

Visual attributes can broadly be classified into two categories:

  • Attributes that add interactivity to your plot
  • Attributes that enhance the visual style of your plot

This section will lay the foundation for both these categories and show how you can fully utilize and exploit Bokeh to get the most out of your plots.

Attributes that add interactivity to the plot

The visual attributes that further enhance the interactivity of your plots are as follows:

  • Hover Tooltip: Lets you point the mouse at a specific point in the plot and displays associated information
  • Selection: Lets you select a region of the plot and colors that region a different color
...

Summary

This chapter has shed light on all the different tools that you can utilize to add layers of interactivity and visually appealing aesthetics to your plots.

You have learned how to use annotations to add titles, legends, and categorical color maps to your plots in order to convey supplemental information about the plot. You also learned how to build all the different types of widget that Bokeh offers in order to make your plots more interactive. Finally, you also learned about visual attributes that add both interactivity and style to your visualizations.

In the next chapter, you will learn how to combine everything you have learned in this chapter and the previous chapters into an interactive application that will enthrall both yourself and the users of your plots!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Data Visualization with Bokeh
Published in: Jun 2018Publisher: PacktISBN-13: 9781789135404
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Kevin Jolly

Kevin Jolly is a formally educated data scientist with a master's degree in data science from the prestigious King's College London. Kevin works as a statistical analyst with a digital healthcare start-up, Connido Limited, in London, where he is primarily involved in leading the data science projects that the company undertakes. He has built machine learning pipelines for small and big data, with a focus on scaling such pipelines into production for the products that the company has built. Kevin is also the author of a book titled Hands-On Data Visualization with Bokeh, published by Packt. He is the editor-in-chief of Linear, a weekly online publication on data science software and products.
Read more about Kevin Jolly