Contour plot Python Tutorial with example

Learn Contour plot Python Tutorial with example. The matplotlib contour plot is also called as level plots and shows the three dimensional surface on two dimensional plane. They will plot the graph with two predictor variables as x, y on y axis . The response is given to the z variable as contours and called as z slices or is response values. There are two inputs as the x and the y such that z=f(x, y). A contour function has two variables and the constant value. The independent variables x and the y are restricted to a rectangular grid called as meshgrid. The meshgrid will create rectangular grid of an array x values and of y values. The matplotlib api contains the contour () and contour () function that will draw contour lines and filled contours both.
The functions will need three parameters as x, y and z.

The counter plot Python are the lines used for the geographical and meteorology

Counter line joints the point of equal height above the grid and the level such as sea level.
Counter line of a function is with two variables curve in which counter plots are created.
In the civil engineering the contour plot will show the topology of building slight.
And also in the mechanical engineering the contour plot will show the stress gradient across surface.
The contour line of function has two variables with curve and has function of constant value.
It is the cross section of the three dimensional graph of function f(x, y) parallel to x, y plane.
The contour line is used in the geographical and mete plot.
The line of function has two variables along with the contrast value.

contour plot

Matplotlibs plty.counterf () method:-

While building the counter plot with matplotlib using the ax.counter() method.
The method is called as follows:-
      ax. contour(X, Y, Z)
Where x and y are two dimensional arrays of x and y points and z is the 2d array point that will determine the “height” of contour. The contour is represented by color in 2D plot.
The code section will include the numpy np.meshgrid () function which will produce the 2D array from two 1D arrays.

The plt.contourf () method is similar to ax. contour method() produce the contour plot that are “filled”.
The lines in the ax. Contour() plot has shaded areas produced by ax.contorf() plot.
Example:-

import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include 
%matplotlib inline

x = np.arange (-3.0, 3.0, 0.1)
y = np.arange (-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)
fig, ax = plt.subplots(fig size=(6,6))
ax. Contour(X, Y, Z)
plt.show ()

Output:- 
contour plot python

Simple count plot:-

The program will produce a filled contour plot of function, labels contours and will provide some custom styling for colors.
Example:-
 
Import numpy as np
Import matplotlib.pyplot as plt
Import matplotlib.cm as cm
X=np.linespace (0, 1,100)
Y=x.copy ()
X, y=np.meshgrid(x,y)
Alpha=np.radians (25)
Cx,cy=0.5,0.5
Sigx sigy=0.2, 0.3
rx=np.cos(alpha)*(x-cx)-np.sin(alpha)*(y-cy)+cx
ry=np.sin(alpha)*(x-cx)+np.cos(alpha)*(y-cy)+cy
z=(rx-cx)*np.exp(((rx-cv)/sigx)**2)*np.exp(-((ry-cy/sigy)**2)
fig=plt.figure()
ax=fig.add_subplot(111)
#reversed greys colourmap for filled contours
Cpf=ax.contourf(x, y, z, 20, cmap=cm, greys_r)
#set the colours of the contours and labels so they are white where the
#contour fill is dark (z<0) and black where it’s light (z>=0)
Colors= [‘w’if level<0 else ‘k’ for level in cpf.levels]
Cp=ax. Contour(x, y, z, 20, colors=colors)
ax. label (cp,fontsize=12,colors=colors)
plt.show ()
 
Output:-
contour plot in python

Example:-

Import numpy as np
Import matplotlib.pyplot as plt
xlist=np.linespace(-3.0,3.0,100)
ylist=np.linespace(-3.0,3.0,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)
fig.colorbar (cp)#Add a colorbar to a plot
ax.set_title(‘Filled contours plot’)
#ax.set_xlabel(‘x(cm)’)
ax.set_ylabel(‘y(cm)’)
plt.show()

contour plot python

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India