Control axis ticks locations using tick locators in matplotlib

matplotlib_tick_locators

Skill - Control axis ticks locations using tick locators in matplotlib

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


Matplotlib is a plotting library tn the scipy ecosystem of libraries.

Please make sure that you covered thepost on basics


Sometimes we may desire to control the location of axis ticks of our subplots. This can be achieved by using TickLocators in matplotlib.

TickLocators help matplotlib to determine the location of ticks

Control spacing between ticks using ‘MultipleLocator’

import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6,7,8]
y = [8,6,4,2,9,7,6,3,1]

# create a plotting area and get the figure, axes handle in return
fig, ax = plt.subplots()

# plot data on the axes handle
ax.plot(x, y)

# set x axis major ticks for multiples of 3 like 0,3,6,9 etc
ax.xaxis.set_major_locator(plt.MultipleLocator(3))

# set x axis minor ticks for multiples of 1 like 0,1,2,3,4 etc
ax.xaxis.set_minor_locator(plt.MultipleLocator(1))

# print the plot
plt.show()

matplotlib_multiplelocator_demo

Fixing the number of ticks on the axis using ‘LinearLocator’

import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6,7,8]
y = [8,6,4,2,9,7,6,3,1]

# create a plotting area and get the figure, axes handle in return
fig, ax = plt.subplots()

# plot data on the axes handle
ax.plot(x, y)

# set number of minor ticks on the axis as 16
ax.xaxis.set_minor_locator(plt.LinearLocator(numticks=16))
# set number of major ticks on the axis as 6
ax.xaxis.set_major_locator(plt.LinearLocator(numticks=6))

# print the plot
plt.show()

matplotlib_linearlocator_demo

Using ‘LogLocator’ for logarithmic scaling of axis

import matplotlib.pyplot as plt
x = [0,20,300,50000,600000,7000000]
y = [8,7,6,3,5,9]

# create a plotting area and get the figure, axes handle in return
fig, ax = plt.subplots()

# plot data on the axes handle
ax.plot(x, y)

# make axis scale as 'log'
ax.set_xscale('log')
# use log locator of major and minor x axis ticks
ax.xaxis.set_major_locator(plt.LogLocator(base = 10.0))

# print the plot
plt.show()

matplotlib_loglocator_demo

Hiding ticks using ‘NullLocator’

import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6,7,8]
y = [8,6,4,2,9,7,6,3,1]

# create a plotting area and get the figure, axes handle in return
fig, ax = plt.subplots()

# plot data on the axes handle
ax.plot(x, y)

# use null locator to hide major and minor ticks
ax.yaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_minor_locator(plt.NullLocator())
ax.xaxis.set_minor_locator(plt.NullLocator())
ax.xaxis.set_major_locator(plt.NullLocator())

# print the plot
plt.show()

matplotlib_nulllocator_demo

Summary of Locators

Locator class Description
NullLocator No ticks
FixedLocator Tick locations are fixed
IndexLocator Locator for index plots (e.g., where x = range(len(y)))
LinearLocator Evenly spaced ticks from min to max
LogLocator Logarithmically ticks from min to max
MultipleLocator Ticks and range are a multiple of base
MaxNLocator Finds up to a max number of ticks at nice locations
AutoLocator (Default.) MaxNLocator with simple defaults.
AutoMinorLocator Locator for minor ticks

tick_locators_illustration
Check out this post for all locators example code

Video

You can the video on this post here


Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/

References


Table of Contents

Comments