如何在 python 中使用 plotly 制作具有 8 种不同颜色八分圆的 3d 立方体视觉效果?

问题描述 投票:0回答:1

如何在 python 中使用 plotly 制作具有 8 种不同颜色八分圆的 3d 立方体视觉效果?

到目前为止,我已经能够使用 matplotlib 制作我想要的立方体,但是我希望能够使用 plotly 重新创建立方体,以便我有一个交互式视觉效果。

3d plotly
1个回答
0
投票

要得到一个由 8 个子立方体组成的立方体,放置在每个八分圆中,在第一个八分圆中定义一个

template_cube
(即对应于 x、y、z ≥ 0 的八分圆),然后在方向上平移这个由数组 T 中的行定义,定义如下。然后八个子立方体中的每一个都被三角化并表示为 go.Mesh3d 的实例:

import numpy as np
import plotly.graph_objects as go

template_cube = np.array([[0, 0, 0],
                          [1, 0, 0],
                          [1, 1, 0],
                          [0, 1, 0],
                          [0, 0, 1],
                          [1, 0, 1],
                          [1, 1, 1],
                          [0, 1, 1]], dtype=float)
triangles=np.array([[0, 1, 2],
       [2, 3, 0],
       [4, 5, 6],
       [6, 7, 4],
       [5, 1, 2],
       [2, 6, 5],
       [4, 0, 3],
       [3, 7, 4],
       [6, 2, 3],
       [3, 7, 6],
       [4, 5, 1],
       [1, 0, 4]], dtype=int)
I, J, K= triangles.T

colors=['#ce84e1',
 '#87479d',
 '#f4709a',
 '#f0a5c1',
 '#f7e3fd',
 '#b14359',
 '#5e0943',
 '#e7d5f9'       
 ]

T= [[0,0,0], [0, 0, -1], [0,-1, 0], [0, -1, -1], 
    [-1, 0, 0 ], [-1, 0, -1], [-1, -1, 0], [-1, -1, -1]]
mdata=[]
for n, tr in enumerate(T):
    vertices= template_cube+tr
    x, y, z = vertices.T
    mdata.append(go.Mesh3d(x=x, y=y, z=z, i=I, j=J, k=K, 
                           vertexcolor = [colors[n]]*8))
fig=go.Figure(mdata)    
fig.update_layout(width=400, height=400,  font_size=11,
                 scene_camera_eye=dict(x=1.7, y=1.7, z=0.9))    
© www.soinside.com 2019 - 2024. All rights reserved.