Open In App

Matplotlib.ticker.FixedLocator Class in Python

Last Updated : 21 Apr, 2020
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.FixedLocator

The matplotlib.ticker.FixedLocator class is a subclass of matplotlib.ticker.Locator class and is used to fix tick locations. If the value of nbins is not equal to None, then the array of all possible positions would be sub-sampled to keep the total number of ticks smaller than or equal to nbins + 1. The sub-sampling is done as to include the absolute value that is smallest. for instance, if zero is included inside the array of possibly values, then it guarantees a chosen tick.

Syntax: class matplotlib.ticker.FixedLocator(locs, nbins=None)

Parameters:

  • locs: It represents the location of the ticks.
  • nbins: It represents the number of bins that the data is to be divided into.

Methods of the class:

  • set_params(self, nbins=None): It is used to set the parameters within the locator.
  • tick_value(self, vmin, vmax): It returns the location of ticks between vmax and vmin.

Example 1:




import numpy as np
import matplotlib.pyplot as plt
import matplotlib
   
  
np.arange(0, 15, 5)
plt.figure(figsize = [6,4])
   
x = np.array([1, 2, 3, 4, 5,
              6, 7, 8, 9, 10,
              11, 12, 13, 14, 15])
  
y = np.array([15, 16, 17, 18
              19, 20, 40, 50
              60, 70, 80, 90
              100, 110, 120])
  
ax = sns.pointplot(x, y,
                   color = 'k',
                   markers = ["."], 
                   scale = 2)
  
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([1,5,8]))
  
plt.show()


Output:

Example 2:




import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker
  
  
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1 * np.pi * t)*np.exp(-t * 0.01)
  
fig, ax = plt.subplots()
plt.plot(t, s)
  
ax1 = ax.twiny()
ax1.plot(t, s)
  
ax1.xaxis.set_ticks_position('bottom')
  
majors = np.linspace(0, 100, 6)
minors = np.linspace(0, 100, 11)
thirds = np.linspace(0, 100, 101)
  
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(majors))
ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minors))
ax1.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([]))
ax1.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(thirds))
  
ax1.tick_params(which ='minor', length = 2)
ax.tick_params(which ='minor', length = 4)
ax.tick_params(which ='major', length = 6)
  
ax.grid(which ='both', axis ='x', linestyle ='--')
  
plt.axhline(color ='green')
  
plt.show()


Output:



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

Similar Reads