How to change ticks label sizes using Python’s Bokeh


Bokeh is one of the data visualization libraries available in python, which allows the users to create the interactive plots, data applications, dashboards in web browsers. The Bokeh provides us varieties of plots and charts such as scatter plot, line plot, bar charts and area charts etc. and also more specialized heat maps and geographic maps. This is an Open source library with the active developer community.

Creating Plots using Bokeh in Python

The Bokeh library provides two main interfaces for creating the plot, one is low level interface which is used to develop the plots from the individual components and the other is high level interface which is used to develop the plots from the simple python code. The high level interface is advanced than the low level interface and provides more intuitive and simpler way to create different types of plots.

Bokeh library supports a wide range of options, which allows the users to control the plots by adding annotations and interactions and also allows us to include the data from variety of sources.

Features of the Bokeh library

The following are the key features of the Bokeh library.

  • It provides the interactive data visualization in the web browsers.

  • The output of this library can be HTML, PNG and SVG.

  • It supports the data of real time and streaming.

  • This also supports the geographic mapping and data exploration.

  • This can be integrated with the libraries such as Numpy and Pandas.

In data visualization and plotting, the ticks are the small marks or indicators that are used in the plots to identify the data being displayed. There are two types of ticks available in plotting. One is the xticks and the other is yticks.

Installing Bokeh library

To use the Bokeh library in python, first we have to install the library by using the below code.

pip install bokeh

On successful installation of the bokeh library in python following output will be generated

pip install bokeh
Collecting bokeh
  Downloading bokeh-3.1.1-py3-none-any.whl (8.3 MB)
     ---------------------------------------- 8.3/8.3 MB 44.2 MB/s eta 0:00:00
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, xyzservices, tzdata, tornado, six, PyYAML, pillow, packaging, numpy, MarkupSafe, python-dateutil, Jinja2, contourpy, pandas, bokeh
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 PyYAML-6.0 bokeh-3.1.1 contourpy-1.0.7 numpy-1.24.3 packaging-23.1 pandas-2.0.1 pillow-9.5.0 python-dateutil-2.8.2 pytz-2023.3 six-1.16.0 tornado-6.3.2 tzdata-2023.3 xyzservices-2023.2.0

Example

In this example we will create the plot using the bokeh library without specifying the tics in the plot. The following is the code which can be used as reference.

In the below code we imported the figure and show functions from the bokeh library, then specified the size of the plot and plotted the scatter plot for the data of x and y variables and finally displayed the created plot.

from bokeh.plotting import figure, show
p = figure(width=600, height=400)
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 6, 1]
p.scatter(x, y)
show(p)

Output

Following is the output of the scatter plot created using the bokeh library.

Example

In the following example, we will specify the ticks by using the NumeralTickFormatter() function of the bokeh library.

from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter
p = figure(width=600, height=400)
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 6, 1]
p.scatter(x, y)
p.yaxis.formatter = NumeralTickFormatter(format="$0,0")
p.xaxis.formatter = NumeralTickFormatter(format = "%0,0")
show(p)

Output

Following is the output of the scatter plot created using the bokeh function with the ticks on x-axis and y-axis.

Example

Let’s see one more example by applying the ticks on the x-axis and y-axis using the NumeralTickFormatter() function.

from bokeh.plotting import figure, show
from bokeh.models import NumeralTickFormatter
import numpy as np
p = figure(width=600, height=400)
x = np.arange(10,40,2)
y = np.random.randn(20)
p.line(x, y)
p.yaxis.formatter = NumeralTickFormatter(format="%0,0")
p.xaxis.formatter = NumeralTickFormatter(format = "$0,0")
show(p)

Output

Following is the output of the ticks using in the plot we plotted using the scatter() and NumeralTickFormatter() functions.

Updated on: 09-Aug-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements