如何在Python中绘制3D表面

时间:2013-10-08 13:41:19

标签: python matplotlib plot 3d geometry-surface

我应该在herehere的任何示例中更改/添加哪些内容才能从以下数据生成3D表面?

[100, 0, 'TF', 0]
[100, 0, 'DF', 0]
[100, 50, 'TF', 0]
[100, 50, 'DF', 0]
[100, 100, 'TF', 0]
[100, 100, 'DF', 0]
[100, 150, 'TF', 0.84496124031007758]
[100, 150, 'DF', 0.87209302325581395]
[100, 200, 'TF', 0.88139534883720927]
[100, 200, 'DF', 0.86201550387596892]
[100, 250, 'TF', 0.87441860465116272]
[100, 250, 'DF', 0.889922480620155]
[100, 300, 'TF', 0.87984496124031009]
[100, 300, 'DF', 0.89922480620155043]
[100, 350, 'TF', 0.92015503875968996]
[100, 350, 'DF', 0.90697674418604657]

注意:我确实根据上面提到的代码尝试了一些方法,但到目前为止我还没有成功。我没有在这里引用我的代码的原因是因为我会让我的问题变得更复杂,实际上是它。

2 个答案:

答案 0 :(得分:0)

所以你的数据不是真正的3D,因为所有的x值是相同的,但这是我编辑你发布的例子的方式。出于某种原因,X希望成为列表列表。也许其他人知道原因。enter image description here

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt

#step = 0.04
#maxval = 1.0
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# create supporting points in polar coordinates
#r = np.linspace(0,1.25,50)
#p = np.linspace(0,2*np.pi,50)
#R,P = np.meshgrid(r,p)
# transform them to cartesian system
#X,Y = R*np.cos(P),R*np.sin(P)

X=[]
for i in range(0,16):
    X.append([])
    X[i].append(100)
print(X)

Y=[]
for i in range(0,8):
    Y.append(50*i)
    Y.append(50*i)
print(Y)

Z=[]
Z.append(0)
Z.append(0)
Z.append(0)
Z.append(0)
Z.append(0)
Z.append(0)
Z.append(0.84496124031007758)
Z.append(0.87209302325581395)
Z.append(0.88139534883720927)
Z.append(0.86201550387596892)
Z.append(0.87441860465116272)
Z.append(0.889922480620155)
Z.append(0.87984496124031009)
Z.append(0.89922480620155043)
Z.append(0.92015503875968996)
Z.append(0.90697674418604657)
print(Z)


#Z = ((R**2 - 1)**2)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.YlGnBu_r)
ax.set_zlim3d(0, 1)
ax.set_xlabel(r'xlabel')
ax.set_ylabel(r'ylabel')
ax.set_zlabel(r'zlabel')
plt.show()

答案 1 :(得分:0)

对x数据进行一点改动会给你一个表面:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

data = [[100, 0, 'TF', 0],
        [100, 0, 'DF', 0],
        [100, 50, 'TF', 0],
        [100, 50, 'DF', 0],
        [100, 100, 'TF', 0],
        [100, 100, 'DF', 0],
        [100, 150, 'TF', 0.84496124031007758],
        [100, 150, 'DF', 0.87209302325581395],
        [100, 200, 'TF', 0.88139534883720927],
        [100, 200, 'DF', 0.86201550387596892],
        [100, 250, 'TF', 0.87441860465116272],
        [100, 250, 'DF', 0.889922480620155],
        [100, 300, 'TF', 0.87984496124031009],
        [100, 300, 'DF', 0.89922480620155043],
        [100, 350, 'TF', 0.92015503875968996],
        [100, 350, 'DF', 0.90697674418604657]
        ]
x = [item[0] for item in data]
# change x here
x = np.linspace(0, 100, num = 16)
y = [item[1] for item in data]
z = [item[3] for item in data]

X, Y = np.meshgrid(x, y)
_z, Z = np.meshgrid(x, z)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.YlGnBu_r)
ax.set_zlim3d(0, 1)
ax.set_xlabel(r'xlabel')
ax.set_ylabel(r'ylabel')
ax.set_zlabel(r'zlabel')
plt.show()
plt.close()

enter image description here