Plotting using contour in Python

Plotting using contour in Python

using contour():- In this section, we discuss plotting using contour function which is used to create contour plots. First of all, moving on to this tutorial you should first read what is Contour plots. We will discuss here some equations which can be implemented in Python using contour(). so in this section, we will discuss how to plot a function of a given equation using contour. we will discuss these functions one by one with code in Python and with output.


Examples using Contour in Python

1. To plot function f(x,y) = 10-x2-ywhen value of z = [1,6,9]

Python Code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x,y=np.meshgrid(x,y)
z=10-x**2-y**2levels=np.array([1,6,9])
cs=plt.contour(x,y,z,levels)
plt.clabel(cs,inline=1,fontsize=9)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =10-x**2-y**2')
plt.show()
Output:
To plot function f(x,y) = 10-x2-y2 when value of z = [1,6,9]
To plot function f(x,y) = 10-x2-ywhen value of z = [1,6,9]


2. To plot function f(x,y) = x2 + y2  when value of z =  [1,6,9]

Python Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x, y = np.meshgrid(x, y)
z = x ** 2 + y ** 2levels = np.array([1, 6, 9])
cs = plt.contour(x, y, z, levels)
plt.clabel(cs, inline=1, fontsize=9)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =x**2+y**2.')
plt.show()
Output:
Level curves of the function f(x,y) =x**2+y**2
Level curves of the function f(x,y) =x**2+y**2













3. To plot function f(x,y) = x3-y when value of z = [1,6]

Python Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x, y = np.meshgrid(x, y)
z = x ** 3 - y
levels = np.array([1, 6])
cs = plt.contour(x, y, z, levels)
plt.clabel(cs, inline=1, fontsize=9)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =x**3-y')
plt.show()
Output:


To plot function f(x,y) = x3-y when value of z = [1,6]
To plot function f(x,y) = x3-y when value of z = [1,6]

















4. To plot function f(x,y) = (x+ y2 ) /4 when value of z = [1,5,8]

Python Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x,y=np.meshgrid(x,y)
z=x**2+y**2/4levels=np.array([1,5,8])
cs=plt.contour(x,y,z,levels)
plt.clabel(cs,inline=1,fontsize=9)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =x**2+y**2/4')
plt.show()
Output:
To plot function f(x,y) = (x2 + y2 )/ 4 when value of z = [1,5,8]
To plot function f(x,y) = (x+ y2 ) /4 when value of z = [1,5,8]













5. To plot function f(x,y) = 4x2+y when value of z = [0,1,3,5]

Python Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x,y=np.meshgrid(x,y)
z=4*(x**2)+y**2levels=np.array([0,1,3,5])
cs=plt.contour(x,y,z,levels)
plt.clabel(cs,inline=20,fontsize=6)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =4*(x**2)+y**2')
plt.show()
Output:

Plotting using contour in Python
To plot function f(x,y) = 4x2+y when value of z = [0,1,3,5]



















6. To plot function f(x,y) = 2-x-y when value of z = [-6,-4,-2,0,2,4,6]  
  
Python Code:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 21)
y = np.linspace(-10, 10, 21)
x,y=np.meshgrid(x,y)
z=2-x-y
levels=np.array([-6,-4,-2,0,2,4,6])
cs=plt.contour(x,y,z,levels)
plt.clabel(cs,inline=1,fontsize=9)
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Level curves of the function f(x,y) =2-x-y')
plt.show()
Output:
To plot function f(x,y) = 2-x-y when value of z = [-6,-4,-2,0,2,4,6]
To plot function f(x,y) = 2-x-y when value of z = [-6,-4,-2,0,2,4,6]  
Summary: In this section, we discuss the code for the. In this code first, we need to import numpy and we used numpy as np then we need to import matplotlib which is necessary to plot a graph. so we defined matplotlib as plt. In this matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. For every pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with x and y labels and also clabel to label the curve of the fucntion, the title of the graph, grid to show grid in the graph etc.