To create a visualization of 3-Dimensional plots in 2-Dimensional space we use contour plots in Matplotlib. Contour plots are also known as Level Plots.
- Suppose there are two variables X and Y and you want to plot them then the response of two variables will be Z and it will be plotted as slices on the X-Y plane due to which contours are also referred to as Z-slices or iso-response.
- We should use contour plot if you want to see how the value of Z changes as a function of two inputs that is X and Y, like this Z = f(X,Y).
- A contour line or isoline of a function of two variables is basically a curve along which the function has a constant value.
- There are two functions in Matplotlib that is
contour()(this is used to draw the contour lines) and
contourf()(this is used to draw filled contours).
Uses of Contour Plot:
There are some uses of contour plot given below:
- To visualize density.
- To visualize the meteorological department.
- To visualize the heights of mountains.
matplotlib.pyplot.contour() is useful when Z = f(X, Y) here, Z changes as a function of input X and Y.
contourf() function is also available in matplotlib which allows us to draw filled contours.
This method returns a QuadContourSet. The required syntax for the same is given below:
matplotlib.pyplot.contour([X, Y, ] Z, [levels], **kwargs)
Let us discuss the parameters of this function:
1. X, Y
This parameter indicates 2-D NumPy arrays with having same shape as Z or same like 1-D arrays in a manner such that
len(Y)==N (where M are rows and N are columns of Z)
This parameter indicates the height values over which the contour is drawn. The shape is (M, N).
This parameter is used to determine the number and position of the contour lines.
Let us cover some Examples of this function.
Simple Contour Lines Example:
In this example we will plot contour lines with the help of
Let’s take another example for contour plot.
Filled Contour Plot Example:
Now let us draw a filled contour with the help of