Open In App

Matplotlib.axes.Axes.locator_params() in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.locator_params() Function

The Axes.locator_params() function in axes module of matplotlib library is used to control behavior of major tick locators.

Syntax: Axes.locator_params(self, axis=’both’, tight=None, **kwargs)

Parameters: This method accepts the following parameters.

  • axis : This parameter is the axis on which to operate
  • .

  • tight : This parameter is passed to autoscale_view. Default is None, for no change.

Return value: This method does not returns any value.

Below examples illustrate the matplotlib.axes.Axes.locator_params() function in matplotlib.axes:

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.gridspec as gridspec
import numpy as np
   
   
plt.rcParams['savefig.facecolor'] = "0.8"
plt.rcParams['figure.figsize'] = 6, 5
   
fig, ax = plt.subplots()
   
ax.plot([1, 2])
   
ax.locator_params("x", nbins = 3)
ax.locator_params("y", nbins = 5)
   
ax.set_xlabel('x-label')
ax.set_ylabel('y-label')
   
fig.suptitle('matplotlib.axes.Axes.locator_params() \
function Example\n\n', fontweight ="bold")
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.gridspec as gridspec
import numpy as np
  
  
arr = np.arange(100).reshape((10, 10))
norm = mcolors.Normalize(vmin = 0., vmax = 100.)
  
pc_kwargs = {'rasterized': True, 'cmap': 'viridis',
             'norm': norm}
  
fig, (ax, ax1) = plt.subplots(1, 2)
  
im = ax.pcolormesh(arr, **pc_kwargs)
fig.colorbar(im, ax = ax, shrink = 0.6)
  
im1 = ax1.pcolormesh(arr, **pc_kwargs)
ax1.locator_params(nbins = 3)
fig.colorbar(im1, ax = ax1, shrink = 0.6)
  
ax.set_title("Without locator_params()")
ax1.set_title("With locator_params()")
  
fig.suptitle('matplotlib.axes.Axes.locator_params() \
function Example\n\n', fontweight ="bold")
plt.show()


Output:



Last Updated : 21 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads