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

Advanced Plotting with Networks, Geo Data, WebGL, and Exporting Plots

Adding interactivity to plots that are used to visualize networks such as social networks and transport networks allows the user to highlight and interact with the specific parts that they are interested in, while ignoring the rest of the network. This is particularly useful when you have a large and complex network.

Interactivity in geographic data is particularly useful when it comes to extracting information about a specific location of interest, instead of looking at the entire plot. Such geo-based visualizations help us zoom into locations and embed these maps into our applications.

When working with large and complex datasets, it is in our best interests to exploit the performance of Bokeh fully in terms of speed. We can do this with WebGL.

Finally, there are certain cases in which we might want to publish...

Technical requirements

Using Bokeh to visualize networks

Networks help you visualize relationships between people and items. For example, a simple network can show you how people are related to each other. This is illustrated for you in the following diagram:

In this network, it is easy to see that Mary and Jon are friends, while Mary and Tim are coworkers. We can also see that Tim and Jon have no relationship at all. Such networks have powerful applications, especially in the field of social networks!

In this section, we are going to learn how to construct an interactive network using Bokeh. On a fundamental level, there are two ways to do this. The first is to construct and visualize these networks using the default straight lines to connect two nodes together, and the next is to construct and visualize these networks by defining a shape for the path that we choose.

...

Visualizing geographic data with Bokeh

In this section, we are going to create a Bokeh visualization for the states in the USA that have the lowest crime rates. In order to do this, we will render a map of the USA and pinpoint the states that have the lowest crime rates.

The first step is to import the required packages:

from bokeh.sampledata import us_states
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool

us_states is a dictionary containing all the information that we need to construct a map of the USA in Bokeh. Additionally, if you wanted to create a map of any other country in Bokeh, you can find the necessary geo data on the web and import the file into your Jupyter Notebook.

The next step is to copy the data into Bokeh so that we can modify the data and delete states that are not significant (if required). Here, we will be deleting...

Using WebGL to improve performance

WebGL is a JavaScript API that is available in almost every popular browser in the market. Fundamentally, the Bokeh plots that are displayed on your browser do so with the help of plugins. This could slow down the rendering of plots, especially if the dataset that is used to render these plots is huge! WebGL renders plots using a GPU, without the need for plugins.

In order to enable WebGL while rendering plots, you simply need to use the code shown here while constructing the figure of the plot:

plot = figure(output_backend = 'webgl')

The only downside to rendering the plot in WebGL is that it does not support every component that Bokeh has to offer. At the moment, only two types of glyphs are supported with WebGL rendering. They are:

  • Circle glyphs
  • Line gyphs

The markers that are supported with WebGL are as follows:

  • asterisk
  • square...

Exporting plots as PNG images

The plot generated above looks both impressive and informative at the same time. We might want to publish this plot on a website or in a magazine/journal as a .PNG image with a higher level of quality. Luckily for us, Bokeh offers this flexibility.

Bokeh can generate such images by using the export function. This function uses a browser called Webkit to save the plot in its memory and capture a screenshot. The dimensions of the generated image will be the same as that of the plot you created.

The first step is to install a dependency that this Bokeh functionality depends upon. This is called Phantomjs. You can install this package using Anaconda with the command shown here:

conda install selenium phantomjs pillow

The next step is to install Selenium using pip. We can do this by using the command shown here:

pip3 install selenium

The final step is...

Summary

This chapter has given you a formal introduction into the world of constructing interactive networks using the Bokeh package. In a systematic way, you learned how to create and structure the process of building networks, both with the default straight path and the explicitly defined path.

You also learned how adding interactivity to geographical data can have a big impact on the viewer of your visualization, as he or she can now extract information from it.

Then, you learned how to use WebGL to render your plots faster and consequently improve the performance of your applications.

Finally, you learned about how you can export these plots in PNG format and render them at a very high quality for publishing.

In the next chapter, you will learn how to build your very own Bokeh application that you can use to analyze the stock market.

...
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