gpt4 book ai didi

python - 如何使用 python 绘制 numpy 数组的 3d 表面?

转载 作者:行者123 更新时间:2023-12-01 04:21:17 25 4
gpt4 key购买 nike

我有一个 2 x 3 的 numpy 数组。如何绘制 2d 数组的 3d 表面,其 x 坐标是数组的列索引,y 坐标是数组的行索引,z 坐标是数组的相应值。像这样:

import numpy as np
twoDArray = np.array([10,8,3],[14,22,36])

# I made a two dimensional array twoDArray
# The first row is [10,8,3] and the second row is [14,22,36]
# There is a function called z(x,y). where x = [0,1], y = [0,1,2]
# I want to visualize the function when
#(x,y) is (0,0), z(x,y) is 10; (x,y) is (0,1), z(x,y) is 8; (x,y) is (0,2), z(x,y) is 3
#(x,y) is (1,0), z(x,y) is 14; (x,y) is (1,1), z(x,y) is 22; (x,y) is (1,2), z(x,y) is 36

所以我只想知道该怎么做。如果能提供代码就好了。

最佳答案

这个问题仍然有点不清楚,但一般来说,从 2d 数组绘制 3d 表面:

import numpy as np
import matplotlib.pyplot as pl
from mpl_toolkits.mplot3d import Axes3D

x,y = np.meshgrid(np.arange(160),np.arange(120))
z = np.random.random(x.shape)

pl.figure()
ax = pl.subplot(111, projection='3d')
ax.plot_surface(x,y,z)

产品:

3d_surface

或者,对于您更新的问题:

x_1d = np.arange(2)
y_1d = np.arange(3)
x,y = np.meshgrid(x_1d,y_1d)
z = np.array([[10,8,3],[14,22,36]])

pl.figure()
ax = pl.subplot(111, projection='3d')
ax.plot_surface(x,y,z.transpose())

关于python - 如何使用 python 绘制 numpy 数组的 3d 表面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com