Prerequisites: Introduction to Bokeh in Python
In this article, we will discuss how to plot multiple plots using Bokeh in Python. We are going to use the row() method of the bokeh.layouts module, it is used in show() method of bokeh.io library as an argument to depict multiple plots in using bokeh.
Syntax:
show(row(fig1,fig2,fig3…..fign))
In which fig1, fig2, etc are objects of the class figure in bokeh.plotting module.
Approach
- Import required modules
- Assign coordinates and depict plots using figure class.
- Use the figure objects as arguments in the row() method.
- Use the show() method to depict the visualization returned from the row()method.
Example 1:
Different plots in the same page
Python3
# import modules from bokeh.io import output_file, show from bokeh.layouts import row from bokeh.plotting import figure # create a new plot fig1 = figure(plot_width = 500 , plot_height = 500 ) fig1.line([ 1 , 2 , 3 , 4 , 5 ], [ 3 , 1 , 2 , 6 , 5 ], line_width = 5 ) # create another plot x = y = list ( range ( 10 )) fig2 = figure(plot_width = 500 , plot_height = 500 ) fig2.circle(x, y, size = 5 ) # depict visualization show(row(fig1, fig2)) |
Output:
Example 2:
Different plots on the same frame
Python3
# import modules from bokeh.io import output_file, show from bokeh.layouts import row from bokeh.plotting import figure import numpy as np import random # create a new plot # instantiating the figure object fig1 = figure(title = "Plot 1" ) # coordinates x = [[[[ 0 , 0 , 1 , 1 ]]], [[[ 2 , 2 , 4 , 4 ], [ 2.5 , 2.5 , 3.5 , 3.5 ]]], [[[ 2 , 0 , 4 ]]]] y = [[[[ 2.5 , 0.5 , 0.5 , 2.5 ]]], [[[ 1 , 0 , 0 , 1 ], [ 0.75 , 0.25 , 0.25 , 0.75 ]]], [[[ 2 , 0 , 0 ]]]] # color values of the polygons color = [ "red" , "purple" , "yellow" ] # fill alpha values of the polygons fill_alpha = 0.5 # plotting the graph fig1.multi_polygons(x, y, color = color, fill_alpha = fill_alpha) # create another plot # coordinates x = np.arange( 5 ) y = x * * 2 z = x * 3 p = np.linspace( 1 , 20 , 7 ) q = np.linspace( 1 , 10 , 7 ) r = np.linspace( 1 , 30 , 5 ) a = np.arange( 31 ) # creating an empty figure with specific plot # width and height fig2 = figure(title = "Plot 2" ) # plotting the points in the form of # circular glyphs fig2.circle(x, y, color = "red" , size = 20 ) # plotting the points in the form of # square glyphs fig2.square(x, z, color = "blue" , size = 15 , alpha = 0.5 ) # plotting the points in the form of # hex glyphs fig2. hex (y, z, color = "green" , size = 10 , alpha = 0.7 ) # drawing a line between the plotted points fig2.line(x, y, color = "green" , line_width = 4 ) # plotting the points in the form of # inverted triangle glyph fig2.inverted_triangle(p, q, color = "yellow" , size = 20 , alpha = 0.4 ) # plotting the points in the form of # diamond glyphs fig2.diamond(x, r, color = "purple" , size = 16 , alpha = 0.8 ) # plotting the points in the form of # cross glyphs fig2.cross(a, a, size = 14 ) # create a third plot # generating the points to be plotted x = [] y = [] for i in range ( 100 ): x.append(i) for i in range ( 100 ): y.append( 1 + random.random()) # parameters of line 1 line_color = "red" line_dash = "solid" legend_label = "Line 1" fig3 = figure(title = "Plot 3" ) # plotting the line fig3.line(x, y, line_color = line_color, line_dash = line_dash, legend_label = legend_label) # plotting line 2 # generating the points to be plotted x = [] y = [] for i in range ( 100 ): x.append(i) for i in range ( 100 ): y.append(random.random()) # parameters of line 2 line_color = "green" line_dash = "dotdash" line_dash_offset = 1 legend_label = "Line 2" # plotting the line fig3.line(x, y, line_color = line_color, line_dash = line_dash, line_dash_offset = line_dash_offset, legend_label = legend_label) # depict visualization show(row(fig1, fig2, fig3)) |
Output:
Example 3:
Multiple plots in a row
Python3
# import modules from bokeh.io import output_file, show from bokeh.layouts import row from bokeh.plotting import figure # assign coordinates x = y = list ( range ( 10 )) xs = [[[[ 0 , 0 , 1 , 1 ]]]] ys = [[[[ 3 , 2 , 2 , 3 ]]]] # create a new plot fig1 = figure(title = "Plot 1" , plot_width = 250 , plot_height = 250 ) fig1.line(x, y, line_width = 25 , color = "lime" ) # create another plot fig2 = figure(title = "Plot 2" , plot_width = 250 , plot_height = 250 ) fig2.circle(x, y, size = 25 , color = "lime" ) # create another plot fig3 = figure(title = "Plot 3" , plot_width = 250 , plot_height = 250 ) fig3.square(x, y, size = 25 , color = "lime" ) # create another plot fig4 = figure(title = "Plot 4" , plot_width = 250 , plot_height = 250 ) fig4.triangle(x, y, size = 25 , color = "lime" ) # create another plot fig5 = figure(title = "Plot 5" , plot_width = 250 , plot_height = 250 ) fig5.multi_polygons(xs, ys, color = "lime" ) # create another plot fig6 = figure(title = "Plot 6" , plot_width = 250 , plot_height = 250 ) fig6.line(x, y, line_dash = "dotted" , color = "lime" ) # depict visualization show(row(fig1, fig2, fig3, fig4, fig5, fig6)) |
Output: