Contour plot Python
In this post, you will learn about the Python matplotlib contour plot with examples.
Contour plot is widely used in astrology, meteorology, physics, and cartography, where contour lines on a topological map indicate elevations that are the same. Contour lines commonly show altitude (like height of a geographical features), but they can also be used to show density, brightness, or electric potential. Because of such wide usage, the matplotlib.pyplot provides a method contour to make it simple for us to draw contour plots.
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 and Y, on the y-axis and a response variable, Z, as contours, i.e., Z = f(X,Y).
Syntax of matplotlib.pyplot.contour()
The basic syntax for creating contour plots is-
plt.contour(X,Y,Z,levels)
It is easy to draw a contour in Python using Matplotlib. For this, first we will have to create a list of x and y points and use these points to form a matrix of z values. After that, we can call the contour() function of the matplotlib.pyplot module and display the plot. A contourf() of matplotlib.pyplot is also available which allows us to draw filled contours.
Code Explanation: Contour plot Python
Here, we have explained the code flow step-by-step, which will help you a lot in understanding the code.
Create x and y
With the help of the numpy linspace() method, we have generated 100 x values between -10 and 10.
xlist = np.linspace(-10, 10, 100)
Similarly, we generated 100 y values ranging from -10 to 10.
ylist = np.linspace(-10, 10, 100)
Make X and Y matrices
The numpy.meshgrid() The numpy.meshgrid() function generates a rectangular grid from two given 1-D arrays representing Cartesian 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)
Python 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-
Python 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 the 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 contour lines with the same colour progression used in the contour() plot by using the 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()
Output of the above code-
Related Articles
How to capture a video in Python OpenCV and savePython OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
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