0

I'm trying to draw a filled surface (square) in matplotlib within a 3d environment. literally a face of a cube. But I am having a hard time with the values for the z coordinates. how can I draw a square that is perpendicular to the axes? I am trying to use plot_surface. here is the code that i have so far:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

X = list(np.linspace(-4, 4, 100))
Y = list(np.linspace(-4, 4, 100))
X, Y = np.meshgrid(X, Y)
Z = np.sin((X**2 + Y**2)/4)
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)

ax.set_xlabel('X')
# ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
# ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
# ax.set_zlim(-100, 100)

plt.show()

Update: I am plotting a square in a 3d environment, but its a wire-frame, but I am not able to fill it in. Is it possible to put imshow in the square? I just want to be able to draw a square in any position(x, y, z).

import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations


fig = plt.figure()
ax = fig.gca(projection='3d')
# ax.set_aspect("equal")

# draw cube
x = [-1, 1]
y = [ 1, 1]
z = [-1, 1]
for s, e in combinations(np.array(list(product(x, y, z))), 2):
    if np.sum(np.abs(s-e)) == x[1]-x[0]:
        ax.plot3D(*zip(s, e), color="b")

ax.set_xlabel('X')
# ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
# ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
# ax.set_zlim(-100, 100)
plt.show()
user169808
  • 503
  • 1
  • 6
  • 27

1 Answers1

0

following the example on this stack question:

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

# create a 2 x 2 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,2), np.linspace(0,1,2))
xx2, yy2 = np.meshgrid(np.linspace(2,3,2), np.linspace(2,3,2))

# create vertices for a rotated mesh (3D rotation matrix)
X =  xx
Y =  1*np.ones(X.shape)
Z =  yy

# create some dummy data (0 x 2) for the image
data = 1*np.ones(X.shape)

# create the figure
fig = plt.figure()

# show the 3D rotated projection
ax2 = fig.add_subplot(111, projection='3d')
ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(data), shade=False)
ax2.plot_surface(xx2, Y, yy2, rstride=1, cstride=1, shade=False)

ax2.set_xlabel('X')
# ax.set_xlim(-40, 40)
ax2.set_ylabel('Y')
# ax.set_ylim(-40, 40)
ax2.set_zlabel('Z')
# ax.set_zlim(-100, 100)
plt.show()

enter image description here

user169808
  • 503
  • 1
  • 6
  • 27