Bokeh – Multiple Plots
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 modulesfrom bokeh.io import output_file, showfrom bokeh.layouts import rowfrom bokeh.plotting import figure # create a new plotfig1 = figure(plot_width=500, plot_height=500)fig1.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5], line_width=5) # create another plotx = y = list(range(10))fig2 = figure(plot_width=500, plot_height=500)fig2.circle(x, y, size=5) # depict visuliaztionshow(row(fig1, fig2)) |
Output:


Example 2:
Different plots on the same frame
Python3
# import modulesfrom bokeh.io import output_file, showfrom bokeh.layouts import rowfrom bokeh.plotting import figureimport numpy as npimport random # create a new plot# instantiating the figure objectfig1 = figure(title="Plot 1") # coordinatesx = [[[[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 poloygonscolor = ["red", "purple", "yellow"] # fill alpha values of the polygonsfill_alpha = 0.5 # plotting the graphfig1.multi_polygons(x, y, color=color, fill_alpha=fill_alpha) # create another plot# coordinatesx = np.arange(5)y = x**2z = x*3p = 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 heightfig2 = figure(title="Plot 2") # plotting the points in the form of# cicular glyphsfig2.circle(x, y, color="red", size=20) # plotting the points in the form of# square glyphsfig2.square(x, z, color="blue", size=15, alpha=0.5) # plotting the points in the form of# hex glyphsfig2.hex(y, z, color="green", size=10, alpha=0.7) # drawing a line between the plotted pointsfig2.line(x, y, color="green", line_width=4) # plotting the points in the form of# inverted triangle glyphfig2.inverted_triangle(p, q, color="yellow", size=20, alpha=0.4) # plotting the points in the form of# diamond glyphsfig2.diamond(x, r, color="purple", size=16, alpha=0.8) # plotting the points in the form of# cross glyphsfig2.cross(a, a, size=14) # create a third plot# generating the points to be plottedx = []y = []for i in range(100): x.append(i)for i in range(100): y.append(1 + random.random()) # parameters of line 1line_color = "red"line_dash = "solid"legend_label = "Line 1" fig3 = figure(title="Plot 3") # plotting the linefig3.line(x, y, line_color=line_color, line_dash=line_dash, legend_label=legend_label) # plotting line 2# generating the points to be plottedx = []y = []for i in range(100): x.append(i)for i in range(100): y.append(random.random()) # parameters of line 2line_color = "green"line_dash = "dotdash"line_dash_offset = 1legend_label = "Line 2" # plotting the linefig3.line(x, y, line_color=line_color, line_dash=line_dash, line_dash_offset=line_dash_offset, legend_label=legend_label) # depict visuliaztionshow(row(fig1, fig2, fig3)) |
Output:



Example 3:
Multiple plots in a row
Python3
# import modulesfrom bokeh.io import output_file, showfrom bokeh.layouts import rowfrom bokeh.plotting import figure # assign coordinatesx = y = list(range(10))xs = [[[[0, 0, 1, 1]]]]ys = [[[[3, 2, 2, 3]]]] # create a new plotfig1 = figure(title="Plot 1", plot_width=250, plot_height=250)fig1.line(x, y, line_width=25, color="lime") # create another plotfig2 = figure(title="Plot 2", plot_width=250, plot_height=250)fig2.circle(x, y, size=25, color="lime") # create another plotfig3 = figure(title="Plot 3", plot_width=250, plot_height=250)fig3.square(x, y, size=25, color="lime") # create another plotfig4 = figure(title="Plot 4", plot_width=250, plot_height=250)fig4.triangle(x, y, size=25, color="lime") # create another plotfig5 = figure(title="Plot 5", plot_width=250, plot_height=250)fig5.multi_polygons(xs, ys, color="lime") # create another plotfig6 = figure(title="Plot 6", plot_width=250, plot_height=250)fig6.line(x, y, line_dash="dotted", color="lime") # depict visuliaztionshow(row(fig1, fig2, fig3, fig4, fig5, fig6)) |
Output:



Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


