0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Create Contour Plot in Matplotlib: Simple Guide

To create a contour plot in matplotlib, use the contour or contourf function with 2D grid data for X, Y, and Z values. These functions draw contour lines or filled contours representing levels of the Z data over the X-Y plane.
๐Ÿ“

Syntax

The basic syntax for creating a contour plot is:

  • contour(X, Y, Z, levels): Draws contour lines.
  • contourf(X, Y, Z, levels): Draws filled contour regions.

Here, X and Y are 2D arrays defining the grid coordinates, Z is a 2D array of values at each grid point, and levels is an optional list or number defining contour levels.

python
import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(-3, 3, 100))
Z = np.sin(X**2 + Y**2)

plt.contour(X, Y, Z, levels=10)
plt.show()
๐Ÿ’ป

Example

This example shows how to create both contour lines and filled contour plots using matplotlib. It uses a grid of points and a function to generate Z values, then plots contours with labeled lines and a color bar.

python
import matplotlib.pyplot as plt
import numpy as np

# Create grid data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-X**2 - Y**2) * np.cos(2 * np.pi * X) * np.sin(2 * np.pi * Y)

# Contour lines
contours = plt.contour(X, Y, Z, levels=15, colors='black')
plt.clabel(contours, inline=True, fontsize=8)

# Filled contours
contour_filled = plt.contourf(X, Y, Z, levels=15, cmap='viridis', alpha=0.7)
plt.colorbar(contour_filled)

plt.title('Contour and Filled Contour Plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
Output
A plot window showing contour lines in black with labels and filled color regions in a smooth gradient from the 'viridis' colormap, with a color bar on the side.
โš ๏ธ

Common Pitfalls

  • Not using 2D arrays for X, Y, and Z causes errors; use np.meshgrid to create grids.
  • Passing 1D arrays directly to contour without meshgrid will fail.
  • Choosing too few or too many levels can make the plot unclear or cluttered.
  • For filled contours, forgetting to add a color bar can confuse interpretation.
python
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X**2 + Y**2)  # Correct: Z is 2D, X and Y are 2D

# Correct usage
plt.contour(X, Y, Z)
plt.show()
๐Ÿ“Š

Quick Reference

Tips for creating contour plots:

  • Use np.meshgrid to create 2D coordinate arrays.
  • contour draws lines; contourf fills areas.
  • Set levels to control contour detail.
  • Add plt.colorbar() for filled contours.
  • Label contours with clabel() for clarity.
โœ…

Key Takeaways

Use np.meshgrid to create 2D grids for X and Y before plotting contours.
Use plt.contour for contour lines and plt.contourf for filled contour plots.
Control the number of contour levels with the levels parameter for better visualization.
Always add plt.colorbar() when using filled contours to show the color scale.
Label contour lines with clabel() to make plots easier to understand.