gpt4 book ai didi

python-3.x - 有3个数组。我需要制作 3d 表面。如何解决?

转载 作者:行者123 更新时间:2023-12-02 02:45:29 24 4
gpt4 key购买 nike

我需要创建 3d 曲面图,绘制区域对两个参数(A 和 C)的依赖性。返回图形面积的函数称为 get_square(param_a, param_c)我有 2 个参数为 C 和 A 的数组

python 3.5我得到了 ValueError:参数 Z 必须是二维的。我不明白我需要做什么。我尝试了很多不同的方式,但每次都失败了

a = [70, 71,72,73,74,75,76,77,78,79,80,81,82,82,83,84,85,86,87,88,89]
c = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result_area = []
for i in range(len(a)):
result_area.append(get_area(a=a[i], c=c[i])
# in my case result_area is [8.37489748679822,
8.414865734605842,
8.425909102940215,
8.409343482533345,
8.36477993434033,
8.291545577425058,
8.19076737834649,
8.060984569963367,
7.899169690316133,
7.709739062145273,
7.483527412013634,
7.22563128509485,
6.923664590863622,
6.579039142980158,
6.180241810498949,
5.723812526395248,
5.195589966465734,
4.561306845225758,
3.7683571369877655,
2.696998299888183]

# then i try to create 3d surface:

from mpl_toolkits.mplot3d import axes3d

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
a = np.array(a)
c = np.array(c)
result = np.array(result_area )

ax.plot_surface(a, c, result_area , rstride=10, cstride=10)

plt.show()

我希望像 https://matplotlib.org/mpl_examples/mplot3d/surface3d_demo3.png 这样的 3d 曲面图(当然,我理解,因为我有一个面积对两个参数的依赖关系图,所以表面会不一样)

最佳答案

试试这个 - 对于 x、y 和 z,您需要矩阵来绘制。特别是对于您的 z 轴,您需要一个矩阵,以便将 x、y 的每个组合映射到 z 坐标。

from mpl_toolkits.mplot3d import axes3d

x = [70, 71,72,73,74,75,76,77,78,79,80,81,82,82,83,84,85,86,87,88]
y = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

def get_area(x, y):
return x+y

x = np.array(x)
y = np.array(y)
X, Y = np.meshgrid(x, y) # creates 2 matrices: one repeats x, the other repeats y
Z = get_area(X, Y)

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z , rstride=10, cstride=10)
plt.show()

参见此处:surface plots in matplotlib

关于python-3.x - 有3个数组。我需要制作 3d 表面。如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430385/

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