Matplotlib – Tick Locators


Matplotlib – Tick Locators



”;


In general graphs and plottings, ticks play a crucial role in representing the scale of x and y-axes through small lines, offering a clear indication of the associated values. Tick locators, on the other hand, define the positions of these ticks along the axis, offering a visual representation of the scale.

The below image represents the major and minor ticks on a graph −


Input

Tick Locators in Matplotlib

Matplotlib provides a mechanism for controlling the positioning of ticks on axes through its tick locators. The matplotlib.ticker
module contains classes for configuring tick locating and formatting. These classes include generic tick locators, Formatters, and domain-specific custom ones. While locators are unaware of major or minor ticks, they are used by the Axis class to support major and minor tick locating and formatting.

Different Tick Locators

The matplotlib provides different tick locator within its ticker module, allowing users to customize the tick positions on axes. Some of the Tick Locators include −

  • AutoLocator
  • MaxNLocator
  • LinearLocator
  • LogLocator
  • MultipleLocator
  • FixedLocator
  • IndexLocator
  • NullLocator
  • SymmetricalLogLocator
  • AsinhLocator
  • LogitLocator
  • AutoMinorLocator
  • Defining Custom Locators

Basic Setup

Before diving into specific tick locators, let”s establish a common setup function to draw the plot with ticks.


import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker

def draw_ticks(ax, title):
   # it shows the bottom spine only
   ax.yaxis.set_major_locator(ticker.NullLocator())
   ax.spines[[''left'', ''right'', ''top'']].set_visible(False)

   ax.xaxis.set_ticks_position(''bottom'')
   ax.tick_params(which=''major'', width=1.00, length=5)
   ax.tick_params(which=''minor'', width=0.75, length=2.5)
   ax.set_xlim(0, 5)
   ax.set_ylim(0, 1)
   ax.text(0.0, 0.2, title, transform=ax.transAxes,
      fontsize=14, fontname=''Monospace'', color=''tab:blue'')

Now, let”s explore the working of each tick locator.

Auto Locator

The AutoLocator and AutoMinorLocator are used for automatically determining the positions of major and minor ticks on an axis, respectively.

Example

This example demonstrates how to use the AutoLocator and AutoMinorLocator to automatically handle the positioning of major and minor ticks on an axis.


# Auto Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="AutoLocator() and AutoMinorLocator()")
ax.xaxis.set_major_locator(ticker.AutoLocator())
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
ax.set_title(''Auto Locator and Auto Minor Locator'')
plt.show()

Output


Output 1

Null Locator

The NullLocator places no ticks on the axis.

Example

Let’s see the following example for working of the NullLocator.


# Null Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="NullLocator()")
ax.xaxis.set_major_locator(ticker.NullLocator())
ax.xaxis.set_minor_locator(ticker.NullLocator())
ax.set_title(''Null Locator (No ticks)'')
plt.show()

Output


Output 2

Multiple Locator

The MultipleLocator() class allows ticks to be positioned at multiples of a specified base, supporting both integer and float values.

Example

The following example demonstrates how to use the MultipleLocator() class.


# Multiple Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="MultipleLocator(0.5)")
ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1))
ax.set_title(''Multiple Locator'')
plt.show()

Output


Output 3

Fixed Locator

The FixedLocator() places ticks at specified fixed locations.

Example

Here is an example of using the FixedLocator() class.


# Fixed Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="FixedLocator([0, 1, 3, 5])")
ax.xaxis.set_major_locator(ticker.FixedLocator([0, 1, 3, 5]))
ax.xaxis.set_minor_locator(ticker.FixedLocator(np.linspace(0.2, 0.8, 4)))
ax.set_title(''Fixed Locator'')
plt.show()

Output


Output 4

Linear Locator

The LinearLocator spaces ticks evenly between specified minimum and maximum values.

Example

Here is an example that applies the Linear Locator to the major and minor ticks of an axes.


# Linear Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="LinearLocator(numticks=3)")
ax.xaxis.set_major_locator(ticker.LinearLocator(3))
ax.xaxis.set_minor_locator(ticker.LinearLocator(10))
ax.set_title(''Linear Locator'')
plt.show()

Output


Output 5

Index Locator

This locator is suitable for index plots, where x = range(len(y)).

Example

Here is an example that uses the index loctator (ticker.IndexLocator() class).


# Index Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="IndexLocator(base=0.5, offset=0.25)")
ax.plot([0]*5, color=''white'')
ax.xaxis.set_major_locator(ticker.IndexLocator(base=0.5, offset=0.25))
ax.set_title(''Index Locator'')
plt.show()

Output


Output 6

MaxN Locator


The MaxNLocator finds up to a maximum number of intervals with ticks at nice locations.

Example

Here is an example of using the MaxNLocator() class for both major and minor ticks.


# MaxN Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="MaxNLocator(n=4)")
ax.xaxis.set_major_locator(ticker.MaxNLocator(4))
ax.xaxis.set_minor_locator(ticker.MaxNLocator(40))
ax.set_title(''MaxN Locator'')
plt.show()

Output


Output 7

Log Locator

The LogLocator is used for spacing ticks logarithmically from min to max.

Example

Let”s see an example of using the Log Locator. It shows the minor tick labels on a log-scale.


# Log Locator
fig, ax = plt.subplots(1,1,figsize=(7,1.5), facecolor=''#eaffff'')
plt.subplots_adjust(bottom=0.3, top=0.6, wspace=0.2, hspace=0.4) 
draw_ticks(ax, title="LogLocator(base=10, numticks=15)")
ax.set_xlim(10**3, 10**10)
ax.set_xscale(''log'')
ax.xaxis.set_major_locator(ticker.LogLocator(base=10, numticks=15))
ax.set_title(''Log Locator'')
plt.show()

Output


Output 8

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *