如何将色彩Map表添加到matplotlib 3d图中

hwamh0ep  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(148)

我想添加颜色Map表来显示沿着x轴的渐变,但不幸的是整个图变成了黑色。我可能缺少一些东西,请帮助我。下面是代码。

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

我已经使用gunplot colormapping方法给予渐变色的三维图,但没有工作。请建议我如何进行。如果有任何其他方法使用,我可以创建一个渐变色与6色,也将是罚款。

chhkpiq4

chhkpiq41#

这里的第一个问题是facecolors参数中的条目数量必须与面的数量相匹配,否则看起来matplotlib福尔斯到选择列表中的第一种颜色-这里是黑色。
第二个问题是,你试图创建一个只有6个面的长方体,但然后用一个光滑的colourmap给它着色。matplotlib中的多边形只能用一种颜色给每个多边形着色。所以你必须细分长方体。
下面是我解决这两个问题的尝试!我在0到10之间创建一组点,然后在这些x位置创建顶点。我还直接从面构造x位置数组,取组成每个面的四个顶点中每个顶点的x位置的平均值。

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Define the vertices of the rectangular rod
N_sub = 50  # Set higher for better resolution; lower for better performance!
vertices = np.concatenate([
    np.array([[x, -0.5, -0.5],
              [x, -0.5, 0.5],
              [x, 0.5, 0.5],
              [x, 0.5, -0.5]])
    for x in np.linspace(0, 10, N_sub + 1)], axis=0)

# Define the faces of the rectangular rod
offset = (N_sub) * 4
faces = np.concatenate([
    [[0, 1, 2, 3]],
    *[np.array([[3, 0, 4, 7],
                [0, 1, 5, 4],
                [1, 2, 6, 5],
                [2, 3, 7, 6]]) + 4 * x for x in range(0, N_sub)],
    [[0 + offset, 1 + offset, 2 + offset, 3 + offset]]
], axis=0)

# Map the x-coordinate of each face to a color in the colormap
x = np.array([np.mean([vertices[idx, 0] for idx in face]) for face in faces])
norm = plt.Normalize(x.min(), x.max())
cmap = plt.get_cmap("gnuplot")
colors = cmap(norm(x))

# Create a Poly3DCollection object with the vertices, faces, and colors
rect = Poly3DCollection(vertices[faces], alpha=0.8, facecolors=colors)

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

希望这有帮助!我不确定我是否能够复制黑边。我尝试做第二个长方体,像你原来的一个,有六个面和facecolor="none",但这抛出了一个错误。
PS:我用plt.get_cmap保存了一个导入。

相关问题