Bokeh – ColumnDataSource

Bokeh – ColumnDataSource ”; Previous Next Most of the plotting methods in Bokeh API are able to receive data source parameters through ColumnDatasource object. It makes sharing data between plots and ‘DataTables’. A ColumnDatasource can be considered as a mapping between column name and list of data. A Python dict object with one or more string keys and lists or numpy arrays as values is passed to ColumnDataSource constructor. Example Below is the example from bokeh.models import ColumnDataSource data = {”x”:[1, 4, 3, 2, 5], ”y”:[6, 5, 2, 4, 7]} cds = ColumnDataSource(data = data) This object is then used as value of source property in a glyph method. Following code generates a scatter plot using ColumnDataSource. from bokeh.plotting import figure, output_file, show from bokeh.models import ColumnDataSource data = {”x”:[1, 4, 3, 2, 5], ”y”:[6, 5, 2, 4, 7]} cds = ColumnDataSource(data = data) fig = figure() fig.scatter(x = ”x”, y = ”y”,source = cds, marker = “circle”, size = 20, fill_color = “grey”) show(fig) Output Instead of assigning a Python dictionary to ColumnDataSource, we can use a Pandas DataFrame for it. Let us use ‘test.csv’ (used earlier in this section) to obtain a DataFrame and use it for getting ColumnDataSource and rendering line plot. from bokeh.plotting import figure, output_file, show import pandas as pd from bokeh.models import ColumnDataSource df = pd.read_csv(”test.csv”) cds = ColumnDataSource(df) fig = figure(y_axis_type = ”log”) fig.line(x = ”x”, y = ”pow”,source = cds, line_color = “grey”) show(fig) Output Print Page Previous Next Advertisements ”;

Bokeh – Plots with Glyphs

Bokeh – Plots with Glyphs ”; Previous Next Any plot is usually made up of one or many geometrical shapes such as line, circle, rectangle, etc. These shapes have visual information about the corresponding set of data. In Bokeh terminology, these geometrical shapes are called gylphs. Bokeh plots constructed using bokeh.plotting interface use a default set of tools and styles. However, it is possible to customize the styles using available plotting tools. Types of Plots Different types of plots created using glyphs are as given below − Line plot This type of plot is useful for visualizing the movements of points along the x-and y-axes in the form of a line. It is used to perform time series analytics. Bar plot This is typically useful for indicating the count of each category of a particular column or field in your dataset. Patch plot This plot indicates a region of points in a particular shade of color. This type of plot is used to distinguish different groups within the same dataset. Scatter plot This type of plot is used to visualize relationship between two variables and to indicate the strength of correlation between them. Different glyph plots are formed by calling appropriate method of Figure class. The Figure object is obtained by following constructor − from bokeh.plotting import figure figure(**kwargs) The Figure object can be customised by various keyword arguments. Sr.No Title Set the title for the plot 1 x_axis_label Set title of x axis 2 y_axis_label Set title for y axis 3 plot_width Set width of figure 4 plot_height Set height of figure Line plot The line() method of Figure object adds a line glyph to the Bokeh figure. It needs x and y parameters as data arrays for showing their linear relationship. from bokeh.plotting import figure, show fig = figure() fig.line(x,y) show(fig) Following code renders a simple line plot between two sets of values in the form Python list objects − from bokeh.plotting import figure, output_file, show x = [1,2,3,4,5] y = [2,4,6,8,10] output_file(”line.html”) fig = figure(title = ”Line Plot example”, x_axis_label = ”x”, y_axis_label = ”y”) fig.line(x,y) show(fig) Output Bar plot The figure object has two different methods for constructing bar plot hbar() The bars are shown horizontally across plot width. The hbar() method has the following parameters − Sr.No y The y coordinates of the centers of the horizontal bars. 1 height The heights of the vertical bars. 2 right The x coordinates of the right edges. 3 left The x coordinates of the left edges. Following code is an example of horizontal bar using Bokeh. from bokeh.plotting import figure, output_file, show fig = figure(plot_width = 400, plot_height = 200) fig.hbar(y = [2,4,6], height = 1, left = 0, right = [1,2,3], color = “Cyan”) output_file(”bar.html”) show(fig) Output vbar() The bars are shown vertically across plot height. The vbar() method has following parameters − Sr.No x The x-coordinates of the centers of the vertical bars. 1 width The widths of the vertical bars. 2 top The y-coordinates of the top edges. 3 bottom The y-coordinates of the bottom edges. Following code displays vertical bar plot − from bokeh.plotting import figure, output_file, show fig = figure(plot_width = 200, plot_height = 400) fig.vbar(x = [1,2,3], width = 0.5, bottom = 0, top = [2,4,6], color = “Cyan”) output_file(”bar.html”) show(fig) Output Patch plot A plot which shades a region of space in a specific color to show a region or a group having similar properties is termed as a patch plot in Bokeh. Figure object has patch() and patches() methods for this purpose. patch() This method adds patch glyph to given figure. The method has the following arguments − 1 x The x-coordinates for the points of the patch. 2 y The y-coordinates for the points of the patch. A simple patch plot is obtained by the following Python code − from bokeh.plotting import figure, output_file, show p = figure(plot_width = 300, plot_height = 300) p.patch(x = [1, 3,2,4], y = [2,3,5,7], color = “green”) output_file(”patch.html”) show(p) Output patches() This method is used to draw multiple polygonal patches. It needs following arguments − 1 xs The x-coordinates for all the patches, given as a “list of lists”. 2 ys The y-coordinates for all the patches, given as a “list of lists”. As an example of patches() method, run the following code − from bokeh.plotting import figure, output_file, show xs = [[5,3,4], [2,4,3], [2,3,5,4]] ys = [[6,4,2], [3,6,7], [2,4,7,8]] fig = figure() fig.patches(xs, ys, fill_color = [”red”, ”blue”, ”black”], line_color = ”white”) output_file(”patch_plot.html”) show(fig) Output Scatter Markers Scatter plots are very commonly used to determine the bi-variate relationship between two variables. The enhanced interactivity is added to them using Bokeh. Scatter plot is obtained by calling scatter() method of Figure object. It uses the following parameters − 1 x values or field names of center x coordinates 2 y values or field names of center y coordinates 3 size values or field names of sizes in screen

Bokeh – Circle Glyphs

Bokeh – Circle Glyphs ”; Previous Next The figure object has many methods using which vectorised glyphs of different shapes such as circle, rectangle, polygon, etc. can, be drawn. Following methods are available for drawing circle glyphs − circle() The circle() method adds a circle glyph to the figure and needs x and y coordinates of its center. Additionally, it can be configured with the help of parameters such as fill_color, line-color, line_width etc. circle_cross() The circle_cross() method adds circle glyph with a ‘+’ cross through the center. circle_x() The circle_x() method adds circle with an ‘X’ cross through the center. Example Following example shows use of various circle glyphs added to Bokeh figure − from bokeh.plotting import figure, output_file, show plot = figure(plot_width = 300, plot_height = 300) plot.circle(x = [1, 2, 3], y = [3,7,5], size = 20, fill_color = ”red”) plot.circle_cross(x = [2,4,6], y = [5,8,9], size = 20, fill_color = ”blue”,fill_alpha = 0.2, line_width = 2) plot.circle_x(x = [5,7,2], y = [2,4,9], size = 20, fill_color = ”green”,fill_alpha = 0.6, line_width = 2) show(plot) Output Print Page Previous Next Advertisements ”;