How to Set Axis Limits bokeh?


Bokeh is a built-in Python library that makes a highly interactive graph for data visualization. Generally, web developers use this library to build powerful dashboards, live app applications, and, simple charts on the web. In Python, we have some bokeh built-in functions like figure(), vbar(), y_range(), etc. to Set Axis Limits bokeh. The various field used this library such as Data Scientists, Data Engineers, and, Web Developers.

Syntax

The following syntax is used in the examples −

figure()

The figure is a built-in method of bokeh library and it is known for a subclass of plot that simply create the plot creation.

vbar()

The vbar() is a built-in method that follows the bokeh library and shows the result by rendering vertical bars with given center coordinates.

grid_line_color()

This is public data attribute of the grid in Bokeh library that set the color of the grid line.

y_range()

The y_range() follows the module named Range1d from the content of bokeh.models that allow us to set the start and end points of the axis.

line()

The line() method set the line of a graph by using some reference parameters like line_width and color.

Installation Required −

pip install bokeh

This command installs all the required dependencies of the bokeh library.

Example 1

In the following example, begin the program by importing the module figure, show from the contents of bokeh.plotting. Then we have two axes- factors(representing the list of different characters) and x (representing the x-axis). Next, use the built-in function figure() that accepts the parameter named y_range which value is factors to set the range on y-axis and store it in the variable plot_ax. Moving ahead to use the built-in method circle as an object reference with variable plot_ax that sets the point circle for the coordinate axes. Finally, it uses the show function that accepts the parameter named plot_ax to get the result.

from bokeh.plotting 
import figure, show
factors = ["a", "b", "c", "d"]
x = [25, 37, 80, 60]
plot_ax = figure(y_range=factors)
plot_ax.circle(x, factors, size=15, fill_color="orange", line_color="green", line_width=3)
show(plot_ax)

Output

Example 2

In the following example, first, import the module named Range1d from the contents of bokeh.models. Then use the function figure in the variable fig to set the plot length of the graph by using two parameters plot_width and plot_height. Next, set the x_range and y_range in the variable fig and set the value by assigning Range1d which will set the axis limits on the x and y-axis. Moving ahead to use the line and circle as an object to the variable fig to plot the graph. Finally, we are printing the output with the help of a built-in method named show() that accepts parameter fig.

from bokeh.plotting 
import figure, show
from bokeh.models import Range1d
fig = figure(plot_width=500, plot_height=500)
fig.x_range = Range1d(20, 25)
fig.y_range = Range1d(35, 100)
fig.line([12, 22, 33, 14, 2], [6, 7, 5, 9, 5], line_width=2, color="red")
fig.circle([1, 2, 3, 4, 5], [56, 82, 10, 20, 26], size=30, color="green", alpha=0.8)
show(fig)

Output

Example 3

In the following example, we will show how to set the limits on x and y-axes. For the x-axis, it will use the built-in function x_range whereas for the y-axis it will use the built-in function y_range. Then it uses another built-in function called a circle that is set to point on coordinate axes. Finally, use the show() function to get the result.

from bokeh.plotting 
import figure, show
from bokeh.models import Range1d
fig = figure(width=500, height=400, x_range=(0, 30))
fig.y_range = Range1d(0, 15)
fig.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(fig)

Output

Example 4

In the following example, start the program by importing the necessary module. Then set the two variables i.e. products- to represent the list of items and price- set the prices of individual products. Next, use the built-in method figure that accepts some parameters −

  • x_range − Set all the list of variable name products to the x-axis limit on the graph.

  • height − It set the value as an integer to set the height of the graph.

  • title − To set the main title.

  • toolbar_location − Fixed the constant position of the graph by setting the value as none.

  • tools − Set the empty double quote that will fix the constant position of the graph.

These all parameters of the figure are stored in the variable p. Then use the vbar() method to set the vertical bar that accepts some parameters- x(set the value as a product to show all the items in the x-axis of a graph figure), top(set the value as the variable price that sets to the limits on the y-axis), and, width(set the width of the bokeh graph). Now set the gridline color by using

# import the module
from bokeh.plotting import figure, show
products = ['PEN', 'NOTEBOOK', 'ERASER', 'SHARPNER', 'GEOMETRY BOX', 'CHARTPAPER']
# set the axis limit
price = [5, 30, 10, 12, 80, 15]
p = figure(x_range=products, height=350, title="PRODUCT AND PRICE GRAPH",
   toolbar_location=None, tools="")
p.vbar(x=products, top=price, width=0.9)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)

Output

Conclusion

We discussed the various ways to set the limits on the x-axis and y-axis using the module bokeh. The bokeh library performs a high level of data visualization that represents the graph very interactive. The alternative library of bokeh are Plotly.js, Matplotlib, Dash, D3. js, and Tableau.

Updated on: 17-Jul-2023

166 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements