Open In App

Matplotlib.ticker.LinearLocator Class in Python

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.LinearLocator

The matplotlib.ticker.LinearLocator class is used to determine the tick locations. At its first call the function tries to set up the number of ticks to make a nice tick partitioning. There after the interactive navigation improves as  the number of ticks get fixed. The preset parameter is used to set locs based on lom, which is a dictionary mapping of vmin, vmax -> locs.

Syntax: class matplotlib.ticker.LinearLocator(numticks=None, presets=None) 

Parameters:

  • numticks: Number of ticks in total.
  • presets: It is used to set locs based on lom, which is a dictionary mapping of vmin, vmax -> locs.

Methods of the class:

  • set_params(self, numticks=None, presets=None): It is used to set parameters within this locator.
  • tick_values(self, vmin, vmax): It returns the values of located ticks between the vmin and vmax.
  • view_limits(self, vmin, vmax): It is used to intelligently choose the view limits.

Example 1: 

Python3




import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker
 
 
xGrid = np.linspace(1-1e-14, 1-1e-16, 30,
                    dtype = np.longdouble)
 
y = np.random.rand(len(xGrid))
 
plt.plot(xGrid, y)
plt.xlim(1-1e-14, 1)
 
loc = matplotlib.ticker.LinearLocator(numticks = 5)
plt.gca().xaxis.set_major_locator(loc)
 
plt.show()


Output:

  

Example 2: 

Python3




import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
 
 
# Setup a plot such that only the bottom
# spine is shown
def setup(ax):
     
    ax.spines['right'].set_color('green')
    ax.spines['left'].set_color('red')
     
    ax.yaxis.set_major_locator(ticker.NullLocator())
    ax.spines['top'].set_color('pink')
    ax.xaxis.set_ticks_position('bottom')
     
    ax.tick_params(which ='major', width = 1.00)
    ax.tick_params(which ='major', length = 5)
    ax.tick_params(which ='minor', width = 0.75)
    ax.tick_params(which ='minor', length = 2.5)
     
    ax.set_xlim(0, 5)
    ax.set_ylim(0, 1)
    ax.patch.set_alpha(0.0)
 
plt.figure(figsize =(8, 6))
n = 8
ax = plt.subplot(n, 1, 4)
setup(ax)
ax.xaxis.set_major_locator(ticker.LinearLocator(3))
ax.xaxis.set_minor_locator(ticker.LinearLocator(31))
 
ax.text(0.0, 0.1, "LinearLocator",
        fontsize = 14,
        transform = ax.transAxes)
 
plt.subplots_adjust(left = 0.05,
                    right = 0.95,
                    bottom = 0.05,
                    top = 1.05)
 
plt.show()


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads