Open In App

Matplotlib.axes.Axes.clabel() in Python

Last Updated : 01 Jul, 2022
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.clabel() Function

The Axes.clabel() function in axes module of matplotlib library is used to label a contour plot.

Syntax:

Axes.clabel(self, CS, *args, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • cs : This parameter is the ContourSet to label.
  • fontsize : This parameter is the size in points.
  • gridsize : This parameter represents the number of hexagons in the x-direction or both direction.
  • colors : This parameter is used to color the label
  • inline : This parameter removes the underlying contour where the label is placed.
  • inline_spacing : This parameter is space in pixels to leave on each side of label when placing inline.
  • marginals : This parameter is used to plot the marginal density as colormapped rectangles along the bottom of the x-axis and left of the y-axis.
  • fmt : This parameter is the format string for the label.
  • manual : This parameter is used to place the contour label using mouse.
  • rightside_up : This parameter is used to rotate the label.
  • use_clabeltext : This parameter is used to create labels. ClabelText recalculates rotation angles of texts.

Returns: This returns the following:

  • labels:This returns the list of Text instances for the labels.

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

Example-1: 

Python3




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
 
delta = 2.5
x = np.arange(-13.0, 13.0, delta)
y = np.arange(-12.0, 12.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2)) * 3
 
fig1, ax1 = plt.subplots()
CS1 = ax1.contour(X, Y, Z)
 
fmt = {}
strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s
ax1.clabel(CS1, CS1.levels, inline = True,
           fmt = fmt, fontsize = 10)
 
ax1.set_title('matplotlib.axes.Axes.clabel() Example')
plt.show()


Output:

  

Example-2: 

Python3




# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
 
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2)) * 3
 
fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation ='bilinear', origin ='lower',
               cmap ="Greens", extent =(-3, 3, -2, 2))
 
levels = np.arange(-1.2, 1.6, 0.2)
CS = ax.contour(Z, levels, origin ='lower', cmap ='spring',
                linewidths = 2, extent =(-3, 3, -2, 2))
zc = CS.collections[6]
plt.setp(zc, linewidth = 4)
 
ax.clabel(CS, levels[1::2], inline = 1, fmt ='% 1.1f',
          fontsize = 14)
 
CB = fig.colorbar(CS, shrink = 0.8, extend ='both')
CBI = fig.colorbar(im, orientation ='horizontal',
                   shrink = 0.8)
 
ax.set_title('matplotlib.axes.Axes.clabel() Example')
plt.show()


Output:

 



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

Similar Reads