Contour Plot Using Python And Matplotlib

Overview:

  • Contour plot is a collection of contour lines.

 

  • Each contour is a curve that is a resultant of cutting a surface by a plane.

 

  • Every contour need not form a curve. Some of the resultant contours can be a straight line as well.

 

  • Here is the formal definition of a contour plot:
    • A level curve of a function f(x,y) is the curve of points (x,y) where f(x,y) is some constant value, on every point of the curve. Different level curves produced for the f(x,y) for different values of c - can be put together as a plot, which is called a level curve plot or a contour plot.

 

  • Every contour line in a contour plot is drawn for different value of z, each value a constant.

 

Applications of Contour Plots:

  • A contour plot in cartography represents levels of equal elevation with respect to a base level.

 

  • A contour line that connects places with the same temperature is called an isotherm. Remember, a level curve of f(x,y) has the same value z in all the points of x,y that curve passes through

 

  • If a level curve is to be drawn for ocean depth where the ocean depth is the same on the places it connects it is called an isobath.

 

Drawing a Contour Plot using Python and Matplotlib:

  • Create a list of x points

 

  • Create a list of y points

 

  • From x and y form a matrix of z values.

 

  • Call the contour() function of matplotlib.pyplot module and display the plot.

 

Example 1:

import numpy as np
import matplotlib.pyplot as plot
import pylab

# List of points in x axis
XPoints     = []

# List of points in y axis
YPoints     = []

# X and Y points are from -6 to +6 varying in steps of 2 
for val in range(-6, 8, 2):
    XPoints.append(val)
    YPoints.append(val)

# Z values as a matrix
ZPoints     = np.ndarray((7,7))

# Populate Z Values (a 7x7 matrix) - For a circle x^2+y^2=z    
for x in range(0, len(XPoints)):
    for y in range(0, len(YPoints)):
        ZPoints[x][y] = (XPoints[x]* XPoints[x]) + (YPoints[y]*YPoints[y])

# Print x,y and z values
print(XPoints)
print(YPoints)
print(ZPoints)

# Set the x axis and y axis limits
pylab.xlim([-10,10])
pylab.ylim([-10,10])

# Provide a title for the contour plot
plot.title('Contour plot')

# Set x axis label for the contour plot
plot.xlabel('X')

# Set y axis label for the contour plot
plot.ylabel('Y')

# Create contour lines or level curves using matplotlib.pyplot module
contours = plot.contour(XPoints, YPoints, ZPoints)

# Display z values on contour lines
plot.clabel(contours, inline=1, fontsize=10)

# Display the contour plot
plot.show()


Output 1:

Contour Plot also called as level curves

Example 2:

Here is the python program that plots the contour plots or level curves for a saddle surface which is a hyperbolic paraboloid.

import numpy as np

import matplotlib.pyplot as plot
import pylab

# List of points in x axis
XPoints     = []

# List of points in y axis
YPoints     = []

# X and Y points are from -4 to +4 varying in steps of 2 
for val in range(-4, 6, 2):
    XPoints.append(val)
    YPoints.append(val)

# Z values as a matrix
ZPoints     = np.ndarray((5,5))

# Populate Z Values (a 5x5 matrix) - For a saddle surface/hyperbolic paraboloid,  x^2-y^2=z    
for x in range(0, len(XPoints)):
    for y in range(0, len(YPoints)):
        ZPoints[x][y] = (XPoints[x]* XPoints[x]) - (YPoints[y]*YPoints[y])

# Print x,y and z values
print(XPoints)
print(YPoints)
print(ZPoints)

# Set the x axis and y axis limits
pylab.xlim([-4,4])
pylab.ylim([-4,4])

# Provide a title for the contour plot
plot.title('Contour plot for Saddle Surface - Hyperbolic Paraboloid')

# Set x axis label for the contour plot
plot.xlabel('X')

# Set y axis label for the contour plot
plot.ylabel('Y')

# Create contour lines for the Hyperbolic Paraboloid using matplotlib.pyplot module
contours = plot.contour(XPoints, YPoints, ZPoints)

# Display z values on contour lines
plot.clabel(contours, inline=1, fontsize=10)

# Display the contour plot
plot.show()

 

Output 2:

Contour plot for a saddle surface - a hyperbolic paraboloid


Copyright 2019 © pythontic.com