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