Open In App

Matplotlib.ticker.LogLocator 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.LogLocator

The matplotlib.ticker.LogLocator class is used to determine the tick locations for log axes. In this class the ticks are placed on the location as : subs[j]*base**i.



Syntax: class matplotlib.ticker.LogLocator(base=10.0, subs=(1.0, ), numdecs=4, numticks=None)

Parameter:



  • subs: It is an optional parameter which is either None, or string or a sequence of floats. It defaults to (1.0, ). It provides the multiples of integer powers of the base at which is used to place ticks. Only at integer powers of the base the default places ticks. The auto and all are the only accepted string values here. The ticks are placed exactly between integer powers with ‘auto’ whereas with “all’ the integers power are accepted. Here None value is equivalent to ‘auto’.

Methods of the class:

Example 1:




import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, LogLocator
  
x = [1, 2, 3, 4, 5, 6,
     7, 8, 9, 10, 11, 12]
  
y = [0.32, 0.30, 0.28, 0.26,
     0.24, 0.22, 0.20, 0.18,
     0.16, 0.14, 0.12, 0.10]
  
fig = plt.figure()
ax1 = fig.add_subplot(111)
  
x_major = MultipleLocator(4)
x_minor = MultipleLocator(1)
  
ax1.xaxis.set_major_locator(x_major)
ax1.xaxis.set_minor_locator(x_minor)
ax1.set_yscale("log")
  
y_major = LogLocator(base = 10)
y_minor = LogLocator(base = 10, subs =[1.1, 1.2, 1.3])
  
ax1.yaxis.set_major_locator(y_major)
ax1.yaxis.set_minor_locator(y_minor)
  
ax1.plot(x, y)
  
plt.show()

Output:

Example 2:




import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator
  
  
x = np.linspace(0, 10, 10)
y = 2**x
  
f = plt.figure()
ax = f.add_subplot(111)
plt.yscale('log')
  
ax.yaxis.set_major_locator(LogLocator(base = 100))
  
ax.plot(x, y)
  
plt.show()

Output:


Article Tags :