pythonmatplotlibcontourf

What does the levels argument in the contourf mean?


This question is about the matploblib contourf function. (https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.contourf.html)

The documentation for the function describes the levels argument as

levels : int or array-like, optional

If array-like, draw contour lines at the specified levels. The values must be in increasing order.

My question is what do these levels of contour mean? Are they referring to the number density of points in the x-y plane?


Solution

  • From Wikipedia:

    A contour line (also isoline, isopleth, or isarithm) of a function of two variables is a curve along which the function has a constant value, so that the curve joins points of equal value.

    So if you have a function f(x,y) a contour at level a is a line through the points where f(x,y) == a. You can provide several levels a1, a2, a3, ... to obtain several contour lines, one at f(x,y) == a1, one at f(x,y) == a2, etc..

    Specify levels in .contour or .contourf

    In the matplotlib syntax,

    plt.contour(x, y, f(x,y), levels=[a1, a2, a3])