Bokeh – Specialized Curves ”; Previous Next The bokeh.plotting API supports methods for rendering following specialised curves − beizer() This method adds a Bézier curve to the figure object. A Bézier curve is a parametric curve used in computer graphics. Other uses include the design of computer fonts and animation, user interface design and for smoothing cursor trajectory. In vector graphics, Bézier curves are used to model smooth curves that can be scaled indefinitely. A “Path” is combination of linked Bézier curves. The beizer() method has following parameters which are defined − 1 x0 The x-coordinates of the starting points. 2 y0 The y-coordinates of the starting points.. 3 x1 The x-coordinates of the ending points. 4 y1 The y-coordinates of the ending points. 5 cx0 The x-coordinates of first control points. 6 cy0 The y-coordinates of first control points. 7 cx1 The x-coordinates of second control points. 8 cy1 The y-coordinates of second control points. Default value for all parameters is None. Example Following code generates a HTML page showing a Bézier curve and parabola in Bokeh plot − x = 2 y = 4 xp02 = x+0.4 xp01 = x+0.1 xm01 = x-0.1 yp01 = y+0.2 ym01 = y-0.2 fig = figure(plot_width = 300, plot_height = 300) fig.bezier(x0 = x, y0 = y, x1 = xp02, y1 = y, cx0 = xp01, cy0 = yp01, cx1 = xm01, cy1 = ym01, line_color = “red”, line_width = 2) Output quadratic() This method adds a parabola glyph to bokeh figure. The function has same parameters as beizer(), except cx0 and cx1. Example The code given below generates a quadratic curve. x = 2 y = 4 xp02 = x + 0.3 xp01 = x + 0.2 xm01 = x – 0.4 yp01 = y + 0.1 ym01 = y – 0.2 x = x, y = y, xp02 = x + 0.4, xp01 = x + 0.1, yp01 = y + 0.2, fig.quadratic(x0 = x, y0 = y, x1 = x + 0.4, y1 = y + 0.01, cx = x + 0.1, cy = y + 0.2, line_color = “blue”, line_width = 3) Output Print Page Previous Next Advertisements ”;
Category: bokeh
Bokeh – Area Plots
Bokeh – Area Plots ”; Previous Next Area plots are filled regions between two series that share a common index. Bokeh”s Figure class has two methods as follows − varea() Output of the varea() method is a vertical directed area that has one x coordinate array, and two y coordinate arrays, y1 and y2, which will be filled between. 1 x The x-coordinates for the points of the area. 2 y1 The y-coordinates for the points of one side of the area. 3 y2 The y-coordinates for the points of the other side of the area. Example from bokeh.plotting import figure, output_file, show fig = figure() x = [1, 2, 3, 4, 5] y1 = [2, 6, 4, 3, 5] y2 = [1, 4, 2, 2, 3] fig.varea(x = x,y1 = y1,y2 = y2) output_file(”area.html”) show(fig) Output harea() The harea() method on the other hand needs x1, x2 and y parameters. 1 x1 The x-coordinates for the points of one side of the area. 2 x2 The x-coordinates for the points of the other side of the area. 3 y The y-coordinates for the points of the area. Example from bokeh.plotting import figure, output_file, show fig = figure() y = [1, 2, 3, 4, 5] x1 = [2, 6, 4, 3, 5] x2 = [1, 4, 2, 2, 3] fig.harea(x1 = x1,x2 = x2,y = y) output_file(”area.html”) show(fig) Output Print Page Previous Next Advertisements ”;
Bokeh – Basic Concepts
Bokeh – Basic Concepts ”; Previous Next Bokeh package offers two interfaces using which various plotting operations can be performed. bokeh.models This module is a low level interface. It provides great deal of flexibility to the application developer in developing visualizations. A Bokeh plot results in an object containing visual and data aspects of a scene which is used by BokehJS library. The low-level objects that comprise a Bokeh scene graph are called Models. bokeh.plotting This is a higher level interface that has functionality for composing visual glyphs. This module contains definition of Figure class. It actually is a subclass of plot class defined in bokeh.models module. Figure class simplifies plot creation. It contains various methods to draw different vectorized graphical glyphs. Glyphs are the building blocks of Bokeh plot such as lines, circles, rectangles, and other shapes. bokeh.application Bokeh package Application class which is a lightweight factory for creating Bokeh Documents. A Document is a container for Bokeh Models to be reflected to the client side BokehJS library. bokeh.server It provides customizable Bokeh Server Tornadocore application. Server is used to share and publish interactive plots and apps to an audience of your choice. Print Page Previous Next Advertisements ”;
Bokeh – Home
Bokeh Tutorial PDF Version Quick Guide Resources Job Search Discussion This tutorial will help you in understanding about Bokeh which is a data visualization library for Python. Here, you will learn about how to use Bokeh to create data applications, interactive plots and dashboards. Audience This tutorial is designed for software programmers who want to learn the basics of Bokeh and its programming concepts in simple and easy way. This tutorial will give you enough understanding on various functionalities of Bokeh with illustrative examples. Prerequisites Before proceeding, we assume that the reader has basic understanding in programming language Python and interactive data visualization. Print Page Previous Next Advertisements ”;
Bokeh – Getting Started
Bokeh – Getting Started ”; Previous Next Creating a simple line plot between two numpy arrays is very simple. To begin with, import following functions from bokeh.plotting modules − from bokeh.plotting import figure, output_file, show The figure() function creates a new figure for plotting. The output_file() function is used to specify a HTML file to store output. The show() function displays the Bokeh figure in browser on in notebook. Next, set up two numpy arrays where second array is sine value of first. import numpy as np import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) To obtain a Bokeh Figure object, specify the title and x and y axis labels as below − p = figure(title = “sine wave example”, x_axis_label = ”x”, y_axis_label = ”y”) The Figure object contains a line() method that adds a line glyph to the figure. It needs data series for x and y axes. p.line(x, y, legend = “sine”, line_width = 2) Finally, set the output file and call show() function. output_file(“sine.html”) show(p) This will render the line plot in ‘sine.html’ and will be displayed in browser. Complete code and its output is as follows from bokeh.plotting import figure, output_file, show import numpy as np import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) output_file(“sine.html”) p = figure(title = “sine wave example”, x_axis_label = ”x”, y_axis_label = ”y”) p.line(x, y, legend = “sine”, line_width = 2) show(p) Output on browser Print Page Previous Next Advertisements ”;
Bokeh – Setting Ranges
Bokeh – Setting Ranges ”; Previous Next Numeric ranges of data axes of a plot are automatically set by Bokeh taking into consideration the dataset under process. However, sometimes you may want to define the range of values on x and y axis explicitly. This is done by assigning x_range and y_range properties to a figure() function. These ranges are defined with the help of range1d() function. Example xrange = range1d(0,10) To use this range object as x_range property, use the below code − fig = figure(x,y,x_range = xrange) Print Page Previous Next Advertisements ”;
Bokeh – Introduction
Bokeh – Introduction ”; Previous Next Bokeh is a data visualization library for Python. Unlike Matplotlib and Seaborn, they are also Python packages for data visualization, Bokeh renders its plots using HTML and JavaScript. Hence, it proves to be extremely useful for developing web based dashboards. The Bokeh project is sponsored by NumFocus https://numfocus.org/. NumFocus also supports PyData, an educational program, involved in development of other important tools such as NumPy, Pandas and more. Bokeh can easily connect with these tools and produce interactive plots, dashboards and data applications. Features Bokeh primarily converts the data source into a JSON file which is used as input for BokehJS, a JavaScript library, which in turn is written in TypeScript and renders the visualizations in modern browsers. Some of the important features of Bokeh are as follows − Flexibility Bokeh is useful for common plotting requirements as well as custom and complex use-cases. Productivity Bokeh can easily interact with other popular Pydata tools such as Pandas and Jupyter notebook. Interactivity This is an important advantage of Bokeh over Matplotlib and Seaborn, both produce static plots. Bokeh creates interactive plots that change when the user interacts with them. You can give your audience a wide range of options and tools for inferring and looking at data from various angles so that user can perform “what if” analysis. Powerful By adding custom JavaScript, it is possible to generate visualizations for specialised use-cases. Sharable Plots can be embedded in output of Flask or Django enabled web applications. They can also be rendered in Jupyter notebooks. Open source Bokeh is an open source project. It is distributed under Berkeley Source Distribution (BSD) license. Its source code is available on https://github.com/bokeh/bokeh. Print Page Previous Next Advertisements ”;
Bokeh – Axes
Bokeh – Axes ”; Previous Next In this chapter, we shall discuss about various types of axes. Sr.No Axes Description 1 Categorical Axes The bokeh plots show numerical data along both x and y axes. In order to use categorical data along either of axes, we need to specify a FactorRange to specify categorical dimensions for one of them. 2 Log Scale Axes If there exists a power law relationship between x and y data series, it is desirable to use log scales on both axes. 3 Twin Axes It may be needed to show multiple axes representing varying ranges on a single plot figure. The figure object can be so configured by defining extra_x_range and extra_y_range properties Categorical Axes In the examples so far, the Bokeh plots show numerical data along both x and y axes. In order to use categorical data along either of axes, we need to specify a FactorRange to specify categorical dimensions for one of them. For example, to use strings in the given list for x axis − langs = [”C”, ”C++”, ”Java”, ”Python”, ”PHP”] fig = figure(x_range = langs, plot_width = 300, plot_height = 300) Example With following example, a simple bar plot is displayed showing number of students enrolled for various courses offered. from bokeh.plotting import figure, output_file, show langs = [”C”, ”C++”, ”Java”, ”Python”, ”PHP”] students = [23,17,35,29,12] fig = figure(x_range = langs, plot_width = 300, plot_height = 300) fig.vbar(x = langs, top = students, width = 0.5) show(fig) Output To show each bar in different colour, set color property of vbar() function to list of color values. cols = [”red”,”green”,”orange”,”navy”, ”cyan”] fig.vbar(x = langs, top = students, color = cols,width=0.5) Output To render a vertical (or horizontal) stacked bar using vbar_stack() or hbar_stack() function, set stackers property to list of fields to stack successively and source property to a dict object containing values corresponding to each field. In following example, sales is a dictionary showing sales figures of three products in three months. from bokeh.plotting import figure, output_file, show products = [”computer”,”mobile”,”printer”] months = [”Jan”,”Feb”,”Mar”] sales = {”products”:products, ”Jan”:[10,40,5], ”Feb”:[8,45,10], ”Mar”:[25,60,22]} cols = [”red”,”green”,”blue”]#,”navy”, ”cyan”] fig = figure(x_range = products, plot_width = 300, plot_height = 300) fig.vbar_stack(months, x = ”products”, source = sales, color = cols,width = 0.5) show(fig) Output A grouped bar plot is obtained by specifying a visual displacement for the bars with the help of dodge() function in bokeh.transform module. The dodge() function introduces a relative offset for each bar plot thereby achieving a visual impression of group. In following example, vbar() glyph is separated by an offset of 0.25 for each group of bars for a particular month. from bokeh.plotting import figure, output_file, show from bokeh.transform import dodge products = [”computer”,”mobile”,”printer”] months = [”Jan”,”Feb”,”Mar”] sales = {”products”:products, ”Jan”:[10,40,5], ”Feb”:[8,45,10], ”Mar”:[25,60,22]} fig = figure(x_range = products, plot_width = 300, plot_height = 300) fig.vbar(x = dodge(”products”, -0.25, range = fig.x_range), top = ”Jan”, width = 0.2,source = sales, color = “red”) fig.vbar(x = dodge(”products”, 0.0, range = fig.x_range), top = ”Feb”, width = 0.2, source = sales,color = “green”) fig.vbar(x = dodge(”products”, 0.25, range = fig.x_range), top = ”Mar”, width = 0.2,source = sales,color = “blue”) show(fig) Output Log Scale Axes When values on one of the axes of a plot grow exponentially with linearly increasing values of another, it is often necessary to have the data on former axis be displayed on a log scale. For example, if there exists a power law relationship between x and y data series, it is desirable to use log scales on both axes. Bokeh.plotting API”s figure() function accepts x_axis_type and y_axis_type as arguments which may be specified as log axis by passing “log” for the value of either of these parameters. First figure shows plot between x and 10x on a linear scale. In second figure y_axis_type is set to ”log” from bokeh.plotting import figure, output_file, show x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0] y = [10**i for i in x] fig = figure(title = ”Linear scale example”,plot_width = 400, plot_height = 400) fig.line(x, y, line_width = 2) show(fig) Output Now change figure() function to configure y_axis_type=’log’ fig = figure(title = ”Linear scale example”,plot_width = 400, plot_height = 400, y_axis_type = “log”) Output Twin Axes In certain situations, it may be needed to show multiple axes representing varying ranges on a single plot figure. The figure object can be so configured by defining extra_x_range and extra_y_range properties. While adding new glyph to the figure, these named ranges are used. We try to display a sine curve and a straight line in same plot. Both glyphs have y axes with different ranges. The x and y data series for sine curve and line are obtained by the following − from numpy import pi, arange, sin, linspace x = arange(-2*pi, 2*pi, 0.1) y = sin(x) y2 = linspace(0, 100, len(y)) Here, plot between x and y represents sine relation and plot between x and y2 is a straight line. The Figure object is defined with explicit y_range and a line glyph representing sine curve is added as follows − fig = figure(title = ”Twin Axis Example”, y_range = (-1.1, 1.1)) fig.line(x, y, color = “red”) We need an extra y range. It is defined as − fig.extra_y_ranges = {“y2”: Range1d(start = 0, end = 100)} To add additional y axis on right side, use add_layout() method. Add a new line glyph representing x and y2 to the figure. fig.add_layout(LinearAxis(y_range_name = “y2″), ”right”) fig.line(x, y2, color = “blue”,
Bokeh – Annotations and Legends ”; Previous Next Annotations are pieces of explanatory text added to the diagram. Bokeh plot can be annotated by way of specifying plot title, labels for x and y axes as well as inserting text labels anywhere in the plot area. Plot title as well as x and y axis labels can be provided in the Figure constructor itself. fig = figure(title, x_axis_label, y_axis_label) In the following plot, these properties are set as shown below − from bokeh.plotting import figure, output_file, show import numpy as np import math x = np.arange(0, math.pi*2, 0.05) y = np.sin(x) fig = figure(title = “sine wave example”, x_axis_label = ”angle”, y_axis_label = ”sin”) fig.line(x, y,line_width = 2) show(p) Output The title’s text and axis labels can also be specified by assigning appropriate string values to corresponding properties of figure object. fig.title.text = “sine wave example” fig.xaxis.axis_label = ”angle” fig.yaxis.axis_label = ”sin” It is also possible to specify location, alignment, font and color of title. fig.title.align = “right” fig.title.text_color = “orange” fig.title.text_font_size = “25px” fig.title.background_fill_color = “blue” Adding legends to the plot figure is very easy. We have to use legend property of any glyph method. Below we have three glyph curves in the plot with three different legends − from bokeh.plotting import figure, output_file, show import numpy as np import math x = np.arange(0, math.pi*2, 0.05) fig = figure() fig.line(x, np.sin(x),line_width = 2, line_color = ”navy”, legend = ”sine”) fig.circle(x,np.cos(x), line_width = 2, line_color = ”orange”, legend = ”cosine”) fig.square(x,-np.sin(x),line_width = 2, line_color = ”grey”, legend = ”-sine”) show(fig) Output Print Page Previous Next Advertisements ”;
Bokeh – Environment Setup
Bokeh – Environment Setup ”; Previous Next Bokeh can be installed on CPython versions 2.7 and 3.5+ only both with Standard distribution and Anaconda distribution. Current version of Bokeh at the time of writing this tutorial is ver. 1.3.4. Bokeh package has the following dependencies − jinja2 >= 2.7 numpy >= 1.7.1 packaging >= 16.8 pillow >= 4.0 python-dateutil >= 2.1 pyyaml >= 3.10 six >= 1.5.2 tornado >= 4.3 Generally, above packages are installed automatically when Bokeh is installed using Python’s built-in Package manager PIP as shown below − pip3 install bokeh If you are using Anaconda distribution, use conda package manager as follows − conda install bokeh In addition to the above dependencies, you may require additional packages such as pandas, psutil, etc., for specific purposes. To verify if Bokeh has been successfully installed, import bokeh package in Python terminal and check its version − >>> import bokeh >>> bokeh.__version__ ”1.3.4” Print Page Previous Next Advertisements ”;