0

I'm trying to plot the surfaces of a polygon generated from arrays of 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()

Here is the output enter image description here

I would like the output to show each side of the polygon as shaded. What am I doing wrong?

NaN
  • 643
  • 1
  • 8
  • 21
  • See [this question](https://stackoverflow.com/questions/9170838/surface-plots-in-matplotlib). If you have a set of points, as opposed to a function `f(x,y) -> z`, it becomes more difficult because there are multiple ways to connect the points. – Calvin Godfrey Jun 06 '19 at 17:25

1 Answers1

-1

Here you go

=^..^=

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()

Output:

enter image description here

Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38