Matplotlib - Contour Plots



A contour plot, also known as a contour map or a level plot, is a graphical representation of a three-dimensional surface on a two-dimensional plane.

In a contour plot, the surface is represented by a series of contour lines. Each contour line connects points of equal value on the surface, showing regions where the function has the same value. These contour lines are drawn at constant intervals or "levels", hence the name "level plot".

Imagine you have a contour plot of temperature across a map. Each contour line represents areas with the same temperature, like 50°F, 60°F, and so on. By looking at the plot, you can easily see where it is hotter or cooler across the map −

Contours Plots

Contour Plot in Matplotlib

You can create contour plots in Matplotlib using the contour() function in the "matplotlib.pyplot" module. This function accepts X and Y coordinates as either 1D or 2D arrays, representing the grid on which the function "Z" is evaluated. "Z" is a 2D array containing the function values corresponding to the grid points defined by X and Y.

Let's start by drawing a basic contour plot.

Basic Contour Plot

A basic 3D contour in Matplotlib shows contour lines that connect points of equal value, representing the levels or "heights" of the data. Each contour line corresponds to a specific value, forming a map-like representation of the dataset.

Example

In the following example, we are create a basic contour plot. We define the x and y coordinates for the grid, and then use a mathematical function to generate the z values. With these x, y, and z values, we create a contour plot using the contour() function −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Creating contour plot
plt.contour(X, Y, Z)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Contour Plot')

# Displaying the plot
plt.show()
Output

Following is the output of the above code −

Basic Contour Plot

Filled Contour Plot

In a filled contour plot in Matplotlib, instead of just showing contour lines, it fills in the areas between the lines with colors, creating a shaded representation of the data surface. Each color represents a different level or "height" of the data, allowing you to easily see the distribution within the dataset.

Example

In here, we create a filled contour plot using the contourf() function, which colors the regions between contour lines −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Creating a filled contour plot
plt.contourf(X, Y, Z)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filled Contour Plot')

# Displaying the plot
plt.show()
Output

On executing the above code we will get the following output −

Filled Contour Plot

Contour Plot with Specific Levels

In a contour plot with specific levels, you specify the levels at which you want the contours to be drawn. Each contour line connects points of equal value, representing different levels or "heights" of the data. This allows you to customize the visualization to highlight specific features or intervals within the dataset.

Example

In this example, we customize the contour plot by specifying specific contour levels using Matplotlib. After generating the data and creating the contour plot, we use the levels parameter in the contour() function to define the contour levels −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Defining contour levels
levels = np.linspace(-1, 1, 20)

# Creating contour plot with specific levels
plt.contour(X, Y, Z, levels=levels)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Contour Plot with Specific Levels')

# Displaying the plot
plt.show()
Output

After executing the above code, we get the following output −

Contour Plot with Specific Levels

Contour Plot with Colorbar

In Matplotlib, a contour plot with a colorbar displays contour lines to show points of equal value in the dataset, and a colorbar alongside the plot to indicate the correspondence between colors and data values. The colorbar acts as a visual guide, helping you to understand the range and distribution of data values represented by different colors in the plot.

Example

Here, we create a contour plot with a colorbar using Matplotlib. After generating the data and creating the contour plot, we add a colorbar to the plot using the colorbar() function. This colorbar provides a visual representation of the z values corresponding to the contour plot −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Creating a contour plot
contour = plt.contour(X, Y, Z)

# Adding colorbar
plt.colorbar(contour, label='Z-values')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Contour Plot with Colorbar')

# Displaying the plot
plt.show()
Output

On executing the above code we will get the following output −

Contour Plot with Colorbar
Advertisements