Matplotlib.pyplot.contour() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.contour() Function

The contour() function in pyplot module of matplotlib library is used to plot contours.

Syntax: matplotlib.pyplot.contour(\*args, data=None, \*\*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.pyplot.contour() function in matplotlib.pyplot:

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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(1.5, 2.5, delta)
y = np.arange(1.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(X - Y))
  
  
CS1 = plt.contour(X, Y, Z)
   
fmt = {}
strs = ['1', '2', '3', '4', '5', '6', '7']
for l, s in zip(CS1.levels, strs):
    fmt[l] = s
plt.clabel(CS1, CS1.levels, inline = True,
           fmt = fmt, fontsize = 10)
  
plt.title('matplotlib.pyplot.contour() Example')
plt.show()

chevron_right


Output:

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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(-3.0, 5.0, delta)
y = np.arange(-1.3, 2.5, delta)
X, Y = np.meshgrid(x, y)
Z = (np.exp(-X**2 - Y**2) - np.exp(-(X - 1)**2 - (Y - 1)**2))
   
fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation ='bilinear',
               origin ='lower',
               cmap ="bone"
               extent =(-3, 3, -2, 2))
  
levels = np.arange(-1.2, 1.6, 0.2)
CS = ax.contour(Z, levels, 
                origin ='lower',
                cmap ='Greens',
                linewidths = 2,
                extent =(-3, 3, -2, 2))
  
zc = CS.collections[6]
plt.setp(zc, linewidth = 2)
   
ax.clabel(CS, levels,
          inline = 1
          fmt ='% 1.1f',
          fontsize = 14)
  
plt.title('matplotlib.pyplot.contour() Example')
plt.show()

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.