I’m sure you’ve seen a contour plot before. At least in the weather data. It is widely used to present meteorological or geographic data. In this article, I will walk you through how to generate contour plots with Python using Matplotlib.
What are the Contour Plots in Data Science?
Contour Plots are used to present density, brightness, and electrical potential. It is widely used in data analysis and machine learning. It shows how a response variable relates to two predictor variables.
Also, Read – Machine Learning Full Course For free.
Contour Plots gives a 2D view where all the points with the same answer are connected by a line. This line is a contour line. The main elements of a contour plot:
- x-axis and y-axis show predictors
- Contour lines to represent the same response values
- Coloured bands that provide a range of response values.
Contour Plots with Python
Now let’s get started with the task of generating Contour Plots with Python using the Matplotlib package. I will get started with this task by importing the necessary libraries that we need for this task:
import numpy as np
import matplotlib.pyplot as plt
import pylab
Code language: Python (python)
Now I will generate some values to generate the plot:
xp = np.arange(-8, 10, 2)
yp = np.arange(-8, 10, 2)
zp = np.ndarray((9,9))
Code language: Python (python)
Now I will replace the random values that we can come across while generating values:
for x in range(0, len(xp)):
for y in range(0, len(yp)):
zp[x][y] = xp[x]**2 + yp[y]**2
Code language: Python (python)
So now we have the data ready let’s start by plotting a very simple contour plot:
plt.figure(figsize=(7, 5))
plt.title('Contour Plot')
contours = plt.contour(xp, yp, zp)
plt.clabel(contours, inline=1, fontsize=12)
plt.show()
Code language: Python (python)

Plotting A Saddle Surface
I’ll do the same procedure with a slightly different formula for the z values:
xp = np.arange(-3, 4)
yp = np.arange(-3, 4)
zp =np.ndarray((7,7))
for x in range(0, len(xp)):
for y in range(0, len(yp)):
zp[x][y] = xp[x]*xp[x] - yp[y]*yp[y]
plt.figure(figsize=(8, 6))
plt.title('Contour plot for saddle surface - Hyperbolic paraboloid')
contours = plt.contour(xp, yp, zp)
plt.clabel(contours, inline=1, fontsize=12)
plt.show()
Code language: Python (python)

Plotting Density Contour Plots
Now I’m going to create a different dataset where the data points are closer to plot a density graph in contour plots. Here are the values of our data:
x = np.linspace(0, 5, 60)
y = np.linspace(0, 5, 48)
Code language: Python (python)
Now as I have created values for x and y in the above code, I will now create values for z by creating a function:
def fn(x, y):
return np.sin(x)**5 + np.cos(y+17)**8
X, Y = np.meshgrid(x, y)
Z = fn(X, Y)
Code language: Python (python)
The data is now ready. Now let’s draw the density ploy and add a colour palette also specifying that we want 15 equally spaced lines drawn in the range:
plt.figure(figsize=(8, 6))
contours = plt.contour(X, Y, Z, 15, cmap = 'RdGy')
plt.clabel(contours, inline=True, fontsize=12)
Code language: Python (python)

There is a function in matplotlib known as contourf. Let’s see what we can do by using this function:
plt.figure(figsize=(14, 10))
plt.contourf(X, Y, Z, 20, cmap = 'RdGy')
plt.colorbar()
Code language: Python (python)

If you’ve never contoured before, hopefully, you will do it now whenever needed. It gives a lot of information and it looks nice. If you are a machine learning practitioner, use a contour plot to visualize the range in different classes or an anomaly in the data.
I hope you liked this article on how to generate contour plots with Python using the matplotlib package. Feel free to ask your valuable questions in the comments section below.