WelcomeÂ to the world of interactive data visualization using the popular Bokeh library in Python.Â As you go through the chapter, you will learn about the following topics:
- What exactly Bokeh is and how it differs from other plotting libraries
- How you can install Bokeh on your local machine
- How to verify your Bokeh installation
- Where you can go for help should things go wrongÂ
- Some key concepts regarding Bokeh's internal workings
Bokeh is an interactive data visualization library in Python that helps users across all levels visualize both simple and complex data from datasets ranging from small to big.Â You can use Bokeh to create both interactive plots and applications that speak to the general public, statisticians, and even business leaders!
You will be required to have Python installed on a system. Finally, to use the Git repository of this book, the user needs to install Git.
The code files of this chapter can be found on GitHub:https://github.com/PacktPublishing/Hands-on-Data-Visualization-with-Bokeh.
Check out the following video to see the code in action:
The first two, Matplotlib and Seaborn, let you plot static plotsâplots that do not change and plots that cannot be interacted with. These plots are useful and add value when performing exploratory data analysis, as they are quick and easy to implement and very fast to execute.Â
The third plotting library, Bokeh, lets you plot interactive plotsâplots that change when the user interacts with them. These plots are useful when you want to give your audience a wide range of options and tools for inferring and looking at data from various angles.Â
- Tornado >= 4.0
If you're using Python 2.7, ensure that you have all the afore mentioned packages along with:
conda install bokeh
You can also install Bokeh using PyPi for Python 2 via the following code:Â
pip install bokeh
You can install Bokeh using PyPi for Python 3 via the following code:
pip3 install bokeh
For the purposes of this book, all plots will be rendered using Bokeh Version 0.12.15. If you already have Bokeh installed and require an update, simply enter the following code in your terminal or shell:
sudo pip3 install bokeh --upgrade
Once you have installed Bokeh, you will want to verify that it is correctly installed. In order to verify the installation and create all your Bokeh plots, you'll need a Jupyter Notebook. If you are not familiar with working with a Jupyter Notebook before or have installed, the following link will provide you with a step-by-step tutorial on how to install and work with Jupyter Notebook:Â http://jupyter.org/install.
You can verify your installation of Bokeh by generating a simple line plot using a Jupyter Notebook with the following code:
from bokeh.plotting import figure, output_file, show #HTML file to output your plot into output_file("bokeh.html") #Constructing a basic line plot x = [1,2,3] y = [4,5,6] p = figure() p.line(x,y) show(p)
This should open up a new tab on your browser with a plot illustrated as follows:Â
In the event that things go wrong with your installation, you have the following two options:
- TheÂ Bokeh mailing list (https://groups.google.com/a/anaconda.com/forum/#!forum/bokeh) is a group on Google that posts questions and queries related to Bokeh, which are then answered by experts who use the package on a regular basis. Joining this group or looking through its frequently asked questions should help you find the answer to your solution.
- You can also submit an issue on the Bokeh GitHub issue trackerÂ (https://github.com/bokeh/bokeh/issues); your issue will usually be solved in within a matter of a few hours, up to a few days.
The following are some key definitions related to Bokeh:
- Application:Â The BokehÂ application is a rendered Bokeh document that runs in the browser
- Glyphs:Â Glyphs are the building blocks of Bokeh, and they are the lines, circles, rectangles, and other shapes that you see on a Bokeh plot
- Server:Â The Bokeh server is used to share and publish interactive plots and apps to an audience of your choice
- Widgets:Â Widgets in Bokeh are the sliders, drop-down menus, and other small tools that you can embed into your plot to add some interactivity
output_file:Â This method is used to output your plot as an
HTMLfile and can be used as illustrated in the following code:Â
output_notebook:Â This is used to output your plot in the Jupyter Notebook you are presently working on and can be used as illustrated in the following code:
The first step to understanding interfaces is to understand what a class and a method are. Think of a class as a vessel that holds different types of cookie together. The vessel in this case is the class and the cookies are the methods that give the vessel some functionality, in our case, asÂ a container for the cookies.Â
Since Python is an object-oriented programming language, it uses classes to group different objects that it creates together.Â
A class by itself is useless unless it has some functionality associated with it. These functionalities are provided to classes by methods.
Bokeh provides a mid-level plotting interface, similar to that of
matplotlibÂ , which is known as
bokeh.plotting.Â The main class in the
bokeh.plottingÂ interface is the
FigureÂ class, which includes methods for adding different kinds of glyphs to a plot.Â
A user can create a
Figure object by using the
figure function, as illustrated in the following code:Â
from bokeh.plotting import figure # create a Figure object p = figure(plot_width=500, plot_height=400, tools="pan,hover")
In Bokeh, the
figure function, as illustrated in the preceding code, is used to initialize and store the contents of your plot. The variable pÂ in the preceding code now holds information about the plot, including its height, width, and the kind of tools the plot will use. SinceÂ
figure is our main class, methods such as line, circle, and so on can be added to our diagram in order to create the plot.
This chapter has given you the exact set of steps required for installing Bokeh on your local machine. It has also given you a glimpse of the key terms that you'll run into as you work your way through this book.Â
Now that Bokeh has been successfully installed on your local machine, you can open up a new Jupyter Notebook to work on your first plots with Bokeh!Â
In the next chapter, you will learn how to create your very first plot using glyphs; you'll see how it lays the foundation for plotting using Bokeh.