A contour plot displays the isolines of a matrix. Isolines are curves where a function of two variables has the same value.
In this recipe, you will learn how to create contour plots.
Contours are represented as contour plots of the matrix Z
, where Z
is interpreted as height with respect to the XY plane. Z
is of minimum size 2 and must contain at least two different values.
The problem with contour plots is that if they are coded without labeling the isolines, they are rendered pretty useless as we cannot decode the high points from the low points or find local minimas.
Here, we need to label the contour as well. The labeling of isolines can be done by using either labels (clabel()
) or colormaps
. If your output medium permits the use of color, colormaps
are preferred because viewers will be able to decode data more easily.
The other risk with contour plots is in choosing the number of isolines to plot. If we choose too many, the plot becomes too dense to decode, and if we go with too few isolines, we lose information and can perceive data differently.
The contour()
function will automatically guess how many isolines to plot, but we also have the ability to specify our own number.
In matplotlib, we draw contour plots using matplotlib.pyplot.contour
.
There are two similar functions: contour()
draws contour lines, and contourf()
draws filled contours. We are going to demonstrate only contour()
, but almost everything is applicable to contourf()
. They understand almost the same arguments as well.
The contour()
function can have different call signatures, depending on what data we have and/or what the properties that we want to visualize are.
Call signature |
Description |
---|---|
|
Plots the contour of |
|
Plots the contour of |
|
Plots the contour of |
|
Plots the contour lines with levels at the values specified in |
|
Fills the |
|
Uses keyword arguments to control common line properties (colors, line width, origin, color map, and so on). |
There exist certain constraints on the dimensionality and shape of X
, Y
, and Z
. For example, X
and Y
can be of two dimensions and of the same shape as Z
. If they are of one dimension, such that the length of X
is equal to the number of columns in Z
, then the length of Y
will be equal to the number of rows in Z
.
In the following code example, we will:
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt defprocess_signals(x,y): return (1 – (x ** 2 + y ** 2)) * np.exp(-y ** 3 / 3) x = np.arange(-1.5, 1.5, 0.1) y = np.arange(-1.5, 1.5, 0.1) # Make grids of points X,Y = np.meshgrid(x, y) Z = process_signals(X, Y) # Number of isolines N = np.arange(-1, 1.5, 0.3) # adding the Contour lines with labels CS = plt.contour(Z, N, linewidths=2, cmap=mpl.cm.jet) plt.clabel(CS, inline=True, fmt='%1.1f', fontsize=10) plt.colorbar(CS) plt.title('My function: $z=(1-x^2+y^2) e^{-(y^3)/3}$') plt.show()
We reached for little helpers from numpy
to create our ranges and matrices.
After we evaluated my_function
into Z
, we simply called contour
, providing Z
and the number of levels for isolines.
At this point, try experimenting with the third parameter in the N arange()
call. For example, instead of N = np.arange(-1, 1.5, 0.3)
, try changing 0.3
to 0.1
or 1
to experience how the same data is seen differently, depending on how we encode the data in a contour plot.
We also added a color map by simply giving it CS
(a matplotlib.contour.QuadContourSet
instance).