
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Working With Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - LaTeX
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Celluloid Library
- Matplotlib - Blitting
- Matplotlib - Toolkits
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Widgets
- Matplotlib - Cursor Widget
- Matplotlib - Annotated Cursor
- Matplotlib - Buttons Widget
- Matplotlib - Check Buttons
- Matplotlib - Lasso Selector
- Matplotlib - Menu Widget
- Matplotlib - Mouse Cursor
- Matplotlib - Multicursor
- Matplotlib - Polygon Selector
- Matplotlib - Radio Buttons
- Matplotlib - RangeSlider
- Matplotlib - Rectangle Selector
- Matplotlib - Ellipse Selector
- Matplotlib - Slider Widget
- Matplotlib - Span Selector
- Matplotlib - Textbox
- Matplotlib Plotting
- Matplotlib - Line Plots
- Matplotlib - Area Plots
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Arrow Demo
- Matplotlib - Fancy Boxes
- Matplotlib - Zorder Demo
- Matplotlib - Hatch Demo
- Matplotlib - Mmh Donuts
- Matplotlib - Ellipse Demo
- Matplotlib - Bezier Curve
- Matplotlib - Bubble Plots
- Matplotlib - Stacked Plots
- Matplotlib - Table Charts
- Matplotlib - Polar Charts
- Matplotlib - Hexagonal bin Plots
- Matplotlib - Violin Plot
- Matplotlib - Event Plot
- Matplotlib - Heatmap
- Matplotlib - Stairs Plots
- Matplotlib - Errorbar
- Matplotlib - Hinton Diagram
- Matplotlib - Contour Plot
- Matplotlib - Wireframe Plots
- Matplotlib - Surface Plots
- Matplotlib - Triangulations
- Matplotlib - Stream plot
- Matplotlib - Ishikawa Diagram
- Matplotlib - 3D Plotting
- Matplotlib - 3D Lines
- Matplotlib - 3D Scatter Plots
- Matplotlib - 3D Contour Plot
- Matplotlib - 3D Bar Plots
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - 3D Vignettes
- Matplotlib - 3D Volumes
- Matplotlib - 3D Voxels
- Matplotlib - Time Plots and Signals
- Matplotlib - Filled Plots
- Matplotlib - Step Plots
- Matplotlib - XKCD Style
- Matplotlib - Quiver Plot
- Matplotlib - Stem Plots
- Matplotlib - Visualizing Vectors
- Matplotlib - Audio Visualization
- Matplotlib - Audio Processing
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Cheatsheet
- Matplotlib - Useful Resources
- Matplotlib - Discussion
Matplotlib - Surface Plots
A surface plot is a way to represent three-dimensional mathematical function or data on a flat, two-dimensional surface. It shows how the value of the function or data changes across two input variables (usually represented on the x and y axes) and how this change affects the output variable (represented on the z-axis).
Imagine you have a table with two columns representing two independent variables, and a third column representing a dependent variable. The surface plot takes these three columns of data and creates a 3D representation where the two independent variables are on the x and y axes, and the dependent variable is represented by the height or color of the surface −

Surface Plots in Matplotlib
We can create a surface plot in Matplotlib using the plot_surface() function from the mpl_toolkits.mplot3d module. This function is useful for visualizing functions of two variables or three-dimensional datasets. It generates a surface plot that provides a more clear view of the data compared to traditional two-dimensional plots.
The plot_surface() Function
The plot_surface() function in Matplotlib accepts x and y coordinates, organized in a grid-like structure using meshgrid, and the corresponding z values determine the height or value at each grid point. The resulting plot gives you a visual representation of the 3D shape of your data.
Following is the syntax of plot_surface() function in Matplotlib −
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black', **kwargs)
Where,
- X is the x-coordinates of the values (2D array or meshgrid).
- Y is the y-coordinates of the values (2D array or meshgrid).
- Z is the z-coordinaes of the values (2D array or meshgrid).
- cmap is the colormap for coloring the surface. It specifies the color mapping for the surface based on the Z values.
- edgecolor is the color of the edges of the surface.
- **kwargs are the additional keyword arguments for customization (e.g., colors, transparency).
Saddle-shaped Surface Plot
The saddle-shaped surface is a type of three-dimensional plot in Matplotlib that visualizes a mathematical surface resembling a saddle or a hyperbolic paraboloid. This surface is defined by the mathematical function z=x2-y2. The plot provides a clear picture of how the function values change across the X and Y dimensions.
Example
In the following example, we are defining a saddle-shaped surface using the "saddle_surface" function and generating data points for the surface over a specified range. We then create a 3D surface plot using the plot_surface() function, displaying the saddle-shaped surface with a "Viridis" colormap and "black" edges −
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D # Defining a saddle-shaped surface def saddle_surface(x, y): return x**2 - y**2 # Generating data points x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = saddle_surface(X, Y) # Creating a surface plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black') ax.set_title('Saddle-shaped Surface') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') plt.show()
Output
After executing the above code, we get the following output −

Gaussian Surface Plot
The Gaussian surface plot in Matplotlib is a three-dimensional representation of a mathematical function known as a Gaussian distribution or bell curve. This type of surface plot provides a visual representation of the function z=e-(x2+y2), where x and y are the input variables.
Example
In here, we are defining a Gaussian surface using the function "gaussian_surface", and then generating a set of data points for this surface over a specified range. We then create a 3D surface plot displaying the Gaussian surface using the plot_surface() function −
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D # Defining a Gaussian surface def gaussian_surface(x, y): return np.exp(-(x**2 + y**2)) # Generating points x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = gaussian_surface(X, Y) # Creating a surface plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='plasma', edgecolor='black') ax.set_title('Gaussian Surface') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') plt.show()
Output
Following is the output of the above code −

Sine and Cosine Surface Plots
The sine and cosine surface plots in Matplotlib represent a three-dimensional visualization of a mathematical function combining sine and cosine terms. These plots show the behaviour of the function z=sin(√x2+y2)+cos(√x2+y2), where x and y are the input variables
Example
Now, we are generating a 3D surface plot by combining the values of the sine and cosine functions over a meshgrid. The resulting surface is formed by adding the heights of the sine and cosine waves at each point in the grid −
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D # Creating a meshgrid x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) # Defining a surface using sine and cosine functions Z = np.sin(np.sqrt(X**2 + Y**2)) + np.cos(np.sqrt(X**2 + Y**2)) # Creating a surface plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black') ax.set_title('Sine and Cosine Surface') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') plt.show()
Output
Output of the above code is as follows −

Random Noise Surface Plot
The random noise surface plot in Matplotlib is a 3D representation of a surface generated with random noise. This type of plot is useful for visualizing data with an element of randomness or variability.
Example
In the example below, we are generating a 3D surface plot representing random noise. The np.random.rand() function creates a grid of random values between 0 and 1, forming the heights of the surface. The plot is displayed using Matplotlib with a "Cividis" colormap and "black" edges, showing a surface with random fluctuations in height across the specified x and y ranges −
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D # Generating random noise for a surface np.random.seed(42) X = np.linspace(-5, 5, 100) Y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(X, Y) Z = np.random.rand(100, 100) # Creating a surface plot fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z, cmap='cividis', edgecolor='black') ax.set_title('Random Noise Surface') ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.set_zlabel('Z-axis') plt.show()
Output
The output obtained is as shown below −
