I want to add the colormap to show the gradient along x axis but unfortunately the whole figure turns black. I might be lacking something kindly help me in this. Here is the code.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.cm as cm
import numpy as np
# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Define the vertices of the rectangular rod
vertices = np.array([
[0, -0.5, -0.5],
[0, -0.5, 0.5],
[0, 0.5, 0.5],
[0, 0.5, -0.5],
[10, -0.5, -0.5],
[10, -0.5, 0.5],
[10, 0.5, 0.5],
[10, 0.5, -0.5]
])
# Define the faces of the rectangular rod
faces = np.array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[0, 1, 5, 4],
[1, 2, 6, 5],
[2, 3, 7, 6],
[3, 0, 4, 7]
])
# Map the x-coordinate of each vertex to a color in the colormap
x = vertices[:, 0]
norm = plt.Normalize(x.min(), x.max())
colors = cm.gnuplot(norm(x))
# Create a Poly3DCollection object with the vertices, faces, and colors
rect = Poly3DCollection(vertices[faces], alpha=0.8, facecolors=colors, edgecolors='black')
# Add the rectangular rod to the plot
ax.add_collection3d(rect)
# Set the limits of the x, y, and z axes
ax.set_xlim([0, 10])
ax.set_ylim([-1, 1])
ax.set_zlim([-1, 1])
# Add labels to the axes
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
# Show the plot
plt.show()
I have used gunplot colormapping methodto give the gradiant color to the 3d plot but nothing works. Kindly suggest me how to proceed. If there is any other method using which i can create a gradiant with 6 colors that would also be fine.