Trusted answers to developer questions

Python Bokeh grid plot

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Like many Python libraries, Bokeh is a large library with complex commands and detailed representations of many types of plots.

Getting started

In order to get started, you need to have the library installed on your computer. Write:

pip install bokeh

Bokeh is used for several kinds of plots namely scatter plots, grid plots, and line plots. Let’s see how to make a simple Scatter Plot in bokeh.

Bokeh grid plot

Grid plots are used to display multiple plots together in order to visualize and compare them. This can be thought of as an extension of​ scatter plots.

## Grid Plot
import bokeh
from bokeh.plotting import figure, output_notebook, show
from bokeh.layouts import gridplot #firstly import gridplot
output_notebook()
from random import seed
from random import randint
seed(1)
x_value=[]
for i in range(20):
x_value.append(i) #fill x with random values.
y_one = [x**0.5 for x in x_value] #sqrt(x)
y_two = [x**2 for x in x_value] #x^2
# gridplot to show graphs of x^2 and sqrt(x)
# paramters of figure
# plot_width - The width of the solution space for plotting.
# plot_height - The height of the solution space for plotting.
# title - This refers to the main heading of our graph.
# x_axis_label - This shows what does x-axis represent.
# y_axis_label - This shows what does y-axis represent.
p1 = figure(title="Bokeh Grid plot Example", x_axis_label='x_value', y_axis_label='y_value',plot_width=500, plot_height=500)
p1.circle(x_value,y_one,size=14,color='red')
p2 = figure(title="Bokeh Grid plot Example", x_axis_label='x_value', y_axis_label='y_value',plot_width=500, plot_height=500)
p2.circle(x_value,y_two,size=14, color='blue')
p3 = gridplot([[p1,p2]], toolbar_location=None)
show(p3)
#Comparison of two plots shown as a grid plot.

For this code, x and y are the data points on the x and y-axis. The figure function creates a space for the data to be plotted. The .circle functions draws the co-ordinates that have a circular shape. These functions have several parameters, but for this example, we have used color and size. Color refers to the color of the co-ordinate (in order to differentiate between two different datasets, we have written color='blue' and color = 'red'). Size refers to the size of the co-ordinates that are to be plotted. The gridplot functions draws​ all these graphs alongside each other. This grid plot shows two plots in a single figure, y=x^2 and y=sqrt(x). These graphs can be visualized together, which makes it easier to differentiate them.

For Further Understanding:

RELATED TAGS

bokeh
bokeh grid plot
bokeh plot
python plot
gridplot
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?