matplotlib 中的曲面图 [英] surface plots in matplotlib

查看:30
本文介绍了matplotlib 中的曲面图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 3 元组列表,表示 3D 空间中的一组点.我想绘制一个覆盖所有这些点的曲面.

I have a list of 3-tuples representing a set of points in 3D space. I want to plot a surface that covers all these points.

mplot3d 包中的 plot_surface 函数要求参数 X、Y 和 Z 为二维数组.plot_surface 是绘制曲面的正确函数吗?如何将数据转换为所需的格式?

The plot_surface function in the mplot3d package requires as arguments X,Y and Z to be 2d arrays. Is plot_surface the right function to plot surface and how do I transform my data into the required format?

data = [(x1,y1,z1),(x2,y2,z2),.....,(xn,yn,zn)]

推荐答案

对于表面,它与 3 元组列表有点不同,您应该在二维数组中为域传入网格.

For surfaces it's a bit different than a list of 3-tuples, you should pass in a grid for the domain in 2d arrays.

如果你只有一个 3d 点列表,而不是一些函数 f(x, y) ->z,那么你会遇到一个问题,因为有多种方法可以将 3d 点云三角剖分成一个表面.

If all you have is a list of 3d points, rather than some function f(x, y) -> z, then you will have a problem because there are multiple ways to triangulate that 3d point cloud into a surface.

这是一个光滑表面的例子:

Here's a smooth surface example:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D  
# Axes3D import has side effects, it enables using projection='3d' in add_subplot
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()

这篇关于matplotlib 中的曲面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆