
The contourf() function fills intervals that are closed at the top(i.e., includes the lowest values). Unlike the MATLAB version, contourf() cannot draw polygon edges. Contour plots are widely used to visualize the mountain’s density, altitudes, or heights by representing its three-dimensional surface in a two-dimensional plane. In this article, we discussed contour plots with examples and implementations. = np.meshgrid(feature_x, feature_y)Īlso Read: Matplotlib Quiver Plot Conclusion: Below is an example to demonstrate the Matplotlib contour() function in Python. The only difference between them is that the contourf() is used to plot filled contours while contour() only plots the contour lines. Matplotlib contourf() v/s contour()īoth contourf() and contour() functions of the Matplotlib library are used for contour plotting in Python. The contour is then plotted to bypass 3 arguments: A, B, A**2+B**2. For three-dimensional contour plotting, module Axes3D from mpl_toolkits.mplot3d needs to be imported specifically. After that, the meshgrid function is used, and A and B are passed inside it. A numpy array is created to store A and B. In this example, numpy and matplotlib library are imported. Example of Matplotlib contourf()Īxes.plot_surface(a,b,a**2+b**2,cmap="rainbow")

Returns a contour plot based on the desired parameters passed as arguments to the contourf() function. For arrays, draw contour lines at the specified levels.

For integer n, use n data intervals, i.e., draw n+1 contour lines. Levels: Determine the numbers and positions of the contour lines/regions. Z: Height values over which the contour is drawn. They must both be 1-D such that len(X) is the number of columns in Z and len(Y) is the number of rows in Z. X, Y: Both the parameters should have the same shape as Z. Syntax of contourf() function: (*args, data=None, **kwargs)Ĭall Signature: contour( Z,, **kwargs) Parameters of Matplotlib Contourf:

x and y: Specifies 2D or 1D numpy array for plotting.In matplotlib, matplotlib.pyplot includes a method contour to make it easy to construct contour plots due to its widespread use.

When we plot X and Y as our variables, the response Z is shown as slices on the X-Y plane, which is why contours are sometimes referred to as Z-slices.Ĭontour plots are commonly used in meteorological departments to illustrate densities, elevations, or mountain heights.
