Plotting using Contourf( ) in Python

Plotting using Contourf() in Python:-

using Contourf():- In this section, we discuss plotting using contourf function which is used to create filled 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 contourf(). so in this section, we will discuss how to plot a function of a given equation using contourf. we will discuss these functions one by one with code in Python and with output.


Examples using Contourf() 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(-4,4,10)
y = np.linspace(-4,4,10)
x,y=np.meshgrid(x,y)
z=10-x**2-y**2levels=np.array([1,6,9])
cs=plt.contour(x,y,z,levels)
cs1=plt.contourf(x,y,z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Surface curve of the function f(x,y) =10-x**2-y**2')
plt.show()
Output:
Surface curve of the function f(x,y) =10-x**2-y**2
   Surface curve of the function f(x,y) =10-x**2-y**2

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(-3,3,10)
y = np.linspace(-3,3,10)
x, y = np.meshgrid(x, y)
z = x ** 2 + y ** 2levels = np.array([1, 6, 9])
cs = plt.contour(x, y, z, levels)
cs1=plt.contourf(x,y,z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('surface curve of the function f(x,y) =x**2+y**2.')
plt.show()
Output:
surface curve of the function f(x,y) =x**2+y**2
       Surface curve 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(-3,3, 21)
y = np.linspace(-3,3, 21)
x, y = np.meshgrid(x, y)
z = x ** 3 - y
levels = np.array([1, 6])
cs = plt.contour(x, y, z, levels)
cs1 = plt.contourf(x, y, z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Surface curve of the function f(x,y) =x**3-y')
plt.show()
Output:
Surface curve of the function f(x,y) =x**3-y
          Surface curve of the function f(x,y) =x**3-y













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(-3,3,10)
y = np.linspace(-3,3,10)
x,y=np.meshgrid(x,y)
z=x**2+y**2/4levels=np.array([1,5,8])
cs=plt.contour(x,y,z,levels)
cs1 = plt.contourf(x, y, z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('surface curve of the function f(x,y) =x**2+y**2/4')
plt.show()
Output:

surface curve of the function f(x,y) =x**2+y**2/4
    surface curve of the function f(x,y) =x**2+y**2/4






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(-3,3,21)
y = np.linspace(-3,3,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)
cs1 = plt.contourf(x, y, z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('surface curve of the function f(x,y) =4*(x**2)+y**2')
plt.show()
Output:
surface curve of the function f(x,y) =4*(x**2)+y**2
   surface curve of the function f(x,y) =4*(x**2)+y**2







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)
cs1 = plt.contourf(x, y, z,levels)
plt.clabel(cs,inline=1,fontsize=9, colors = 'red')
plt.grid(True)
plt.axis('scaled')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('surface curve of the function f(x,y) =2-x-y')
plt.show()
Output:
surface curve of the function f(x,y) =2-x-y
                           surface curve of the function f(x,y) =2-x-y
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. 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. In this Contourf() for create filled Contour Plots.