Open In App

Surface plots and Contour plots in Python

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Surface plots and contour plots are visualization tools used to represent three-dimensional data in two dimensions. They are commonly used in mathematics, engineering and data analysis to understand the relationships between three variables. In this article, we will understand about surface and contour plots in Python

Surface plots

It is a representation of three-dimensional dataset and it describes a functional relationship between two independent variables X and Z and a dependent variable Y. It shows overall shape of data and make it easy to see the trend behind the data. They are used to:

  • Visualise loss functions in machine learning and deep learning
  • Visualise store or state value functions in reinforcement learning

Creating 3D surface Plot

To create a 3D surface plot in Python we use the plot_surface() function from matplotlib 3D module. Syntax is:

ax.plot_surface(X, Y, Z)

where X and Y are 2D arrays of points while Z is a 2D array of heights. Now let' see how to create a simple surface plot in python:

  • a=np.arange(-1, 1, 0.02) and b=np.arange(-1, 1, 0.02) create 1D arrays for the x and y coordinates.
  • a, b= np.meshgrid(a, b) generates 2D grid arrays from a and b, representing all possible (x, y) coordinate pairs.
  • axes.plot_surface(a, b, a**2 + b**2, cmap='virdis') plots the surface of the function f(a,b) a2 +b2 where the height at each (x, y) point is calculated by the function.
Python
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

a = np.arange(-1, 1, 0.02)
b = np.arange(-1, 1, 0.02)

a, b = np.meshgrid(a, b)

fig = plt.figure()
axes = fig.add_subplot(111, projection='3d')

axes.plot_surface(a, b, a**2 + b**2, cmap='viridis')

plt.show()

Output:

Surface-plot
Surface Plot

Countour plots

Contour plots also known as level plots which are a way to represent three-dimensional data into two dimensions. Instead of showing data points it show "contours" or "levels" of constant values. These plots are useful for visualizing things like density, altitude or temperature at different points on a plane. They are widely used in fields like:

  • Meteorology (to show weather patterns)
  • Geophysics (for mapping terrain)
  • Data analysis (for multivariate analysis)

Creating Contour plots

To create a contour plot, we use the contour() function which works when Z= f(X,Y). This means that the height (Z) depends on the values of X and Y. The syntax is as follows:

matplotlib.pyplot.contour(X, Y, Z, [levels], **kwargs)

Where:

  • X and Y are 2D arrays of the x and y coordinates
  • Z contains the height values over which the contour lines are drawn
  • levels determine the number and position of the contour lines

Let's see how to create contour plot using matplotlib.

Python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

a = np.arange(-1, 1, 0.02)
b = np.arange(-1, 1, 0.02)

a, b = np.meshgrid(a, b)

fig = plt.figure()
axes = fig.add_subplot(111, projection='3d')

axes.contour(a, b, a**2 + b**2)

plt.show()

Output:

Contour-plot
Contour plot

In summary both surface plots and contour plots are valuable tools for visualizing three-dimensional data. Surface plots help us to see the shape of the data while contour plots provide a clear view of data relationships in two dimensions


Next Article

Similar Reads