Sunday, November 17, 2024
Google search engine
HomeLanguagesMatplotlib.axes.Axes.contour() in Python

Matplotlib.axes.Axes.contour() in Python

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.contour() Function

The Axes.contour() function in axes module of matplotlib library is used to Plot contours. contourdraw contour lines.

Syntax:

Axes.contour(self, *args, data=None, **kwargs)
contour([X, Y, ] Z, [levels], **kwargs)

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

  • X, Y: These parameter are the coordinates of the values in Z.
  • Z : This parameter is the height values over which the contour is drawn.
  • levels : This parameter is used to determine the numbers and positions of the contour lines / regions.

Returns: This returns the following:

  • c :This returns the QuadContourSet.

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

Example-1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib
  
delta = 0.15
x = np.arange(-0.5, 2.5, delta)
y = np.arange(-1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2))
  
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.contour() Example')
plt.show()


Output:

Example-2:




# Implementation of matplotlib function
import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
  
delta = 0.25
x = np.arange(-5.0, 5.0, delta)
y = np.arange(-3.3, 5.5, 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 ='Blues',
                linewidths = 2, extent =(-3, 3, -2, 2))
zc = CS.collections[6]
plt.setp(zc, linewidth = 4)
  
ax.clabel(CS, levels, inline = 1, fmt ='% 1.1f',
          fontsize = 14)
ax.set_title('matplotlib.axes.Axes.contour() Example')
plt.show()


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments