Open In App

Matplotlib.ticker.MultipleLocator Class in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

matplotlib.ticker.MultipleLocator

The matplotlib.ticker.MultipleLocator class is used for setting a tick for every integer multiple of a base within the view interval.



Syntax: class matplotlib.ticker.MultipleLocator(base=1.0)

Methods of the class:



Example 1:




import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
  
  
x = [0, 5, 9, 10, 15]
y = [0, 1, 2, 3, 4]
  
tick_spacing = 1
  
fig, ax = plt.subplots(1, 1)
ax.plot(x, y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
  
plt.show()

Output:

Example 2:




import matplotlib.pyplot as plt
import matplotlib.ticker
  
  
plt.plot([-1.5, 0, 1.5], [1, 3, 2])
ax = plt.gca()
  
func = lambda x, pos: str(x).rstrip('0').rstrip('.')
  
ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocator(0.25))
ax.xaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(func))
  
plt.show()

Output:


Article Tags :