How to Create Smooth 3D Surfaces from Scattered Data Using Matplotlib?

Barbara Streisand
Release: 2024-10-26 07:11:02
Original
142 people have browsed it

How to Create Smooth 3D Surfaces from Scattered Data Using Matplotlib?

Creating Surface Plots with Matplotlib

Considering a list of 3-tuples denoting points in 3D space, the task is to generate a surface covering these points.

Using plot_surface from the mplot3d package requires input data in the form of 2D arrays for X, Y, and Z. To transform the given data structure, there are certain considerations to make.

In the case of surfaces, unlike line plots, you need to define a grid covering the domain using 2D arrays. When working with only a list of 3D points, triangulation becomes essential. This is because there are multiple possible triangulations for a given point cloud.

For a smooth surface, the following approach can be taken:

<code class="python">import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random

def fun(x, y):
    return x**2 + y

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array(fun(np.ravel(X), np.ravel(Y)))
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()</code>
Copy after login

This code defines a grid using meshgrid, generates the corresponding Z values, and creates the surface plot using plot_surface. The resulting surface provides a smooth representation of the underlying data.

The above is the detailed content of How to Create Smooth 3D Surfaces from Scattered Data Using Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!