Python Contour Plot Examples
A contour plot is a graphical technique for representing a 3-dimensional surface by plotting constant z slices, called contours, on a 2-dimensional format. It graphs two predictor variables X Y on the y-axis and a response variable Z as contours, i.e., Z = f(X,Y).
The basic syntax for creating contour plots is -
It is easy to draw contour in Python using Matplotlib. For this, first we will have to create a list of x and y points and using these points we will form a matrix of z values. After that, we can call the contour() function of matplotlib.pyplot module and display the plot.
Here, we have explained the code flow step by step which help you a lot in understanding the code -
Create x and y
With the help of numpy linspace() method, we have generated 100 x values between -10 and 10.
xlist = np.linspace(-10, 10, 100)
Similary, we have generated 100 y values between -10 and 10.
ylist = np.linspace(-10, 10, 100)
Make X and Y matrices
The numpy.meshgrid() function is used to create a rectangular grid out of two given 1-D arrays representing the Cartesian indexing or Matrix indexing.
X, Y = np.meshgrid(xlist, ylist)
Compute Z value
Here, we have computed the Z value using np.sqrt() method.
Z = np.sqrt(X**2 + Y**2)
Plot contour map
At last, use the ax.contourf() method to plot the contour map and filled with 100 levels.
cp = ax.contourf(X, Y, Z)
Contour Plot Example
import numpy as np import matplotlib.pyplot as plt xlist = np.linspace(-10, 10, 100) ylist = np.linspace(-10, 10, 100) X, Y = np.meshgrid(xlist, ylist) Z = np.sqrt(X**2 + Y**2) fig,ax=plt.subplots(1,1) cp = ax.contourf(X, Y, Z) # Add a colorbar to a plot fig.colorbar(cp) ax.set_title('Filled Contours Plot') plt.show()
The above code returns the following output -
Contour plot of Random Number
We can also create a contour plot from a random number matrix. Here, the contour() function draws contour lines, taking a 2D array as input. In this case, it's a matrix of 20x60 random elements. The number of level lines to draw is chosen automatically, but we can also specify it as an additional parameter in contour() function.
import numpy as np import matplotlib.pyplot as plt matx = np.random.rand(20, 60) ct = plt.contour(matx) plt.colorbar(); plt.show()
The above code returns a filled contour plot -
We can also fill the spaces between the contours lines with the same color progression used in the contour() plot by using contourf() function, like -
ct = plt.contourf(matx)
Python Plot Ellipse
Here, we draw several ellipses and then call clabel() to display the selected levels. We used the NumPy meshgrid() function to get the coordinate matrices, X and Y, from the two coordinate vectors, x and y.
import matplotlib.pyplot as plt import numpy as np x = np.arange(-3, 3, 0.02) y = np.arange(-3, 3, 0.02) X, Y = np.meshgrid(x, y) ellipses = X*X/7 + Y*Y/5 - 1 cnt = plt.contour(ellipses) plt.clabel(cnt); plt.colorbar() plt.show()
The output of this code is shown in the following image -
Related ArticlesHow to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Simple Face Detection Program in Python OpenCV
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples