【问题标题】:Painting a cube画一个立方体
【发布时间】:2013-09-17 21:09:18
【问题描述】:

我已经举了这个例子,我想制作一个纯黑色的立方体,有什么想法可以在立方体的侧面添加一些颜色吗?

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
from numpy import sin, cos


fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("auto")
ax.set_autoscale_on(True)


#dibujar cubo
r = [-10, 10]
for s, e in combinations(np.array(list(product(r,r,r))), 2):
    if np.sum(np.abs(s-e)) == r[1]-r[0]:
        ax.plot3D(*zip(s,e), color="b")
plt.show()

【问题讨论】:

    标签: python colors matplotlib cube


    【解决方案1】:

    这是在 Matplotlib 中生成参数立方体的定义集合。它比您的 sn-p 更复杂,并且可以以紧凑的方式完成,但这种实现提供了相当多的灵活性,例如任意段数、人脸排除等......

    更多信息(如文档字符串)可在Colour 存储库中找到。

    # Required import for following computations.
    from __future__ import division
    
    import numpy as np
    
    from mpl_toolkits.mplot3d import Axes3D
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    from matplotlib.pyplot import figure, show
    
    
    def quad(plane='xy', origin=None, width=1, height=1, depth=0):
        u, v = (0, 0) if origin is None else origin
    
        plane = plane.lower()
        if plane == 'xy':
            vertices = ((u, v, depth),
                        (u + width, v, depth),
                        (u + width, v + height, depth),
                        (u, v + height, depth))
        elif plane == 'xz':
            vertices = ((u, depth, v),
                        (u + width, depth, v),
                        (u + width, depth, v + height),
                        (u, depth, v + height))
        elif plane == 'yz':
            vertices = ((depth, u, v),
                        (depth, u + width, v),
                        (depth, u + width, v + height),
                        (depth, u, v + height))
        else:
            raise ValueError('"{0}" is not a supported plane!'.format(plane))
    
        return np.array(vertices)
    
    
    def grid(plane='xy',
             origin=None,
             width=1,
             height=1,
             depth=0,
             width_segments=1,
             height_segments=1):
        u, v = (0, 0) if origin is None else origin
    
        w_x, h_y = width / width_segments, height / height_segments
    
        quads = []
        for i in range(width_segments):
            for j in range(height_segments):
                quads.append(
                    quad(plane, (i * w_x + u, j * h_y + v), w_x, h_y, depth))
    
        return np.array(quads)
    
    
    def cube(plane=None,
             origin=None,
             width=1,
             height=1,
             depth=1,
             width_segments=1,
             height_segments=1,
             depth_segments=1):
        plane = (('+x', '-x', '+y', '-y', '+z', '-z')
                 if plane is None else
                 [p.lower() for p in plane])
        u, v, w = (0, 0, 0) if origin is None else origin
    
        w_s, h_s, d_s = width_segments, height_segments, depth_segments
    
        grids = []
        if '-z' in plane:
            grids.extend(grid('xy', (u, w), width, depth, v, w_s, d_s))
        if '+z' in plane:
            grids.extend(grid('xy', (u, w), width, depth, v + height, w_s, d_s))
    
        if '-y' in plane:
            grids.extend(grid('xz', (u, v), width, height, w, w_s, h_s))
        if '+y' in plane:
            grids.extend(grid('xz', (u, v), width, height, w + depth, w_s, h_s))
    
        if '-x' in plane:
            grids.extend(grid('yz', (w, v), depth, height, u, d_s, h_s))
        if '+x' in plane:
            grids.extend(grid('yz', (w, v), depth, height, u + width, d_s, h_s))
    
        return np.array(grids)
    
    
    canvas = figure()
    axes = Axes3D(canvas)
    
    quads = cube(width_segments=4, height_segments=4, depth_segments=4)
    
    # You can replace the following line by whatever suits you. Here, we compute
    # each quad colour by averaging its vertices positions.
    RGB = np.average(quads, axis=-2)
    # Setting +xz and -xz plane faces to black.
    RGB[RGB[..., 1] == 0] = 0
    RGB[RGB[..., 1] == 1] = 0
    # Adding an alpha value to the colour array.
    RGBA = np.hstack((RGB, np.full((RGB.shape[0], 1), .85)))
    
    collection = Poly3DCollection(quads)
    collection.set_color(RGBA)
    axes.add_collection3d(collection)
    
    show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2020-08-19
      • 1970-01-01
      相关资源
      最近更新 更多