In this article, we will be learning about how to set axis limits to a plot in bokeh. When we are plotting a graph with a set of values, then X-limits and Y-limits for the points are automatically created. But bokeh allows us to set those axis limits according to our choice.
So, at first, we need to know that in order to set Axis limits to both X and Y axis, we need to import a package from our bokeh.models module which is known as range1d. Range1d enables us to set axis limits to the plot of our choice.
We can either use google colab or we can use any text editor in our local device to do the above implementation. In order to use a text-editor on our local device, we need to open the command prompt at first and write the following code.
pip install bokeh
After installation, now we are ready to proceed to the main implementation.
In the code below, we are creating an empty plot with the limits of X-axis and Y-Axis according to our choice.
Code:
Python3
# importing figure and show from # bokeh.plotting from bokeh.plotting import figure, show # importing range1d from # bokeh.models in order to change # the X-Axis and Y-Axis ranges from bokeh.models import Range1d # Determining the plot height # and plot width fig = figure(plot_width = 500 , plot_height = 500 ) # With the help of x_range and # y_range functions we are setting # up the range of both the axis fig.x_range = Range1d( 0 , 10 ) fig.y_range = Range1d( 5 , 15 ) # Thus an empty plot is created # where the ranges of both the # axes are custom set by us. show(fig) |
Output:
Explanation:
As we can clearly see from the code, that we have set X-Axis limits from 0-10 and Y-Axis Limits from 5-15. The thing has been implemented in the graph shown above.
Example 2:
In the second example, we are setting the X and Y Axis limits of our own, and then we are pointing a set of points in that range in the graph. The below code shows the above implementation.
Python3
# importing figure and show from # bokeh.plotting from bokeh.plotting import figure, show # importing range1d from # bokeh.models in order to change # the X-Axis and Y-Axis ranges from bokeh.models import Range1d # Determining the plot height # and plot width fig = figure(plot_width = 620 , plot_height = 600 ) # With the help of x_range and # y_range functions we are setting # up the range of both the axis fig.x_range = Range1d( 20 , 25 ) fig.y_range = Range1d( 35 , 100 ) # Creating two graphs in a # single figure where we are # plotting the points in the range # set by us fig.line([ 21 , 20 , 23 , 24 , 22 ], [ 36 , 72 , 51 , 90 , 56 ], line_width = 2 , color = "green" ) fig.circle([ 21 , 20 , 23 , 24 , 22 ], [ 36 , 72 , 51 , 90 , 56 ], size = 20 , color = "green" , alpha = 0.5 ) # Showing the plot show(fig) |
Output:
Explanation:
Since this is a customized axis made by us, the size of the graph will be totally dependent on the axis limits. For example: if the axis limits for both the axis is larger, then the graph plotted above will be smaller than the one shown now.