如何绘制多边形每边的曲面?

2024-09-28 05:18:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制由x, y, z数组生成的多边形的曲面

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

print("numpy version: " + np.__version__)

# [x, y, z] coordinates
p1 = [322697.1875, 3663966.5, -30000.0]
p2 = [325054.34375, 3663966.5, -30000.0]
p3 = [325054.34375, 3665679.5, -30000.0]
p4 = [322697.1875, 3665679.5, -30000.0]
p5 = [322697.1875, 3663966.5, -27703.123046875]
p6 = [325054.34375, 3663966.5, -27703.154296875]
p7 = [325054.34375, 3665679.5, -27703.70703125]
p8 = [322697.1875, 3665679.5, -27703.673828125]

points = [p1, p2, p3, p4, p5, p6, p7, p8]

points = np.array(points)
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
X, Y = np.meshgrid(x, y)

zr = np.tile(z, [8, 1])

fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection = '3d')
ax.plot_surface(X, Y, zr, alpha=0.5)
plt.show()

这是输出 enter image description here

我想输出显示为着色多边形的每一边。我做错什么了?你知道吗


Tags: importnumpyversionasnpplt多边形points
1条回答
网友
1楼 · 发布于 2024-09-28 05:18:54

给你

=^..^=

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

# setup data
p0 = [0, 0, 0]
p1 = [1, 1, 1]
p2 = [2, 2, 2]
p3 = [3, 3, 3]
p4 = [4, 4, 4]
p5 = [5, 5, 5]
p6 = [6, 6, 6]
p7 = [7, 7, 7]
p8 = [8, 8, 8]

# create data array
points = [p0, p1, p2, p3, p4, p5, p6, p7, p8]
points = np.array(points)

# get array co-ordinates
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
# create mesh for X and Y set points
X, Y = np.meshgrid(x, y)
# create 0 surface
Z1 = np.zeros_like(X)
# create 8 surface
Z2 = np.full_like(X, 8)

# plot data
fig = plt.figure(figsize=(16, 10))
ax = fig.gca(projection='3d')
# setup each surface
ax.plot_surface(X, Y, Z1, alpha=0.3)
ax.plot_surface(X, Z1, Y, alpha=0.3)
ax.plot_surface(Z1, X, Y, alpha=0.3)
ax.plot_surface(X, Y, Z2, alpha=0.3)
ax.plot_surface(X, Z2, Y, alpha=0.3)
ax.plot_surface(Z2, X, Y, alpha=0.3)
plt.show()

输出:

enter image description here

相关问题 更多 >

    热门问题