Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw cubes by python

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to draw a cube python, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

Each column of the cube has a different color:

# Import librariesimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as np # Create axisaxes = [5pint 5] # Create Datadata = np.ones (axes, dtype=np.bool) # Controll Tranperencyalpha = 0.9 # Control colourcolors = np.empty (axes + [4], dtype=np.float32) colors [0] = [1,0,0, alpha] # redcolors [1] = [0,1,0, alpha] # greencolors [2] = [0,0,1, alpha] # bluecolors [3] = [1,1,0] Alpha] # yellowcolors [4] = [1,1,1, alpha] # grey # Plot figurefig = plt.figure () ax = fig.add_subplot (111111projection='3d') # Voxels is used to customizations of# the sizes, positions and colors.ax.voxels (data, facecolors=colors, edgecolors='grey')

Each side of the cube has different colors:

Import matplotlib.pyplot as pltimport numpy as np def generate_rubik_cube (nx, ny, nz): "" generate a Rubik's cube of specified size according to input: param nx:: param ny:: param nz:: return: "# prepare some coordinates n_voxels = np.ones ((nx + 2, ny + 2, nz + 2) Dtype=bool) # generate gap size = np.array (n_voxels.shape) * 2 filled_2 = np.zeros (size-1, dtype=n_voxels.dtype) filled_2 [:: 2,: 2,: 2,: 2] = n_voxels # narrow the gap # build a matrix of voxels vertex control grids # x, y, z are all 6x6x8 For the voxels mesh, 3x3x4 small squares, a total of 6x6x8 vertices. # here / / 2 is the essence. Convert the index range from [0 1 2 3 4 5] to [0 0 1 1 22] so that the vertex range x, y, z = np.indices (np.array (filled_2.shape) + 1) .astype (float) / / 2 # 3x6x6x8 of each square can be set separately. Among them, x _ :]-= 0.94 y [:,-1,:]-= 0.94 z [:,:,-1]-= 0.94 # remove leftovers filled_2 [0,0,:] = 0 filled_2 [0,-1,:] = 0 filled_2 [- 1,0,:] = 0 filled_2 [- 1,:] = 0 filled_2 [:, 0 0] = 0 filled_2 [:, 0,-1] = 0 filled_2 [:,-1, 0] = 0 filled_2 [:,-1,-1] = 0 filled_2 [0,:, 0] = 0 filled_2 [0,:,-1] = 0 filled_2 [- 1,:, 0] = 0 filled_2 [- 1,: -1] = 0 # give different colors to the six sides of the Rubik's cube colors = np.array (['# ffd400', "# fffffb", "# f47920", "# d71345", "# 145b7d", "# 45b97c"]) facecolors = np.full (filled_2.shape,'# 77787b') # set a gray tone # facecolors = np.zeros (filled_2.shape, dtype='U7') facecolors [:,: -1] = colors [0] # Yellow facecolors [:,:, 0] = colors [1] # White facecolors [:, 0,:] = colors [2] # left orange facecolors [:,-1,:] = colors [3] # right red facecolors [0,:,:] = colors [4] # pre-blue facecolors [- 1,: :] = colors [5] # back green ax = plt.figure (). Add_subplot (projection='3d') ax.voxels (x, y, z, filled_2, facecolors=facecolors) plt.show () if _ _ name__ ='_ _ main__': generate_rubik_cube (4,4,4)

Color perspective cube:

From _ future__ import divisionimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfrom mpl_toolkits.mplot3d.art3d import Poly3DCollectionfrom matplotlib.pyplot import figure, showdef 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 planetellum. 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 wrapx, hype = width / width_segments Height / height_segments quads = [] for i in range (width_segments): for j in range (height_segments): quads.append (quad (plane, (I * wattx + u, j * hogy + v), wimpx, houry, 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,'- z') if plane is None else [p.lower () for p in plane]) u, v, w = (0,0,0) if origin is None else origin washes, hashes, dudes = width_segments Height_segments, depth_segments grids = [] if'- z' in plane: grids.extend (grid ('xy', (u, w), width, depth, v, winters, dudes)) if' + z' in plane: grids.extend (grid ('xy', (u, w), width, depth, v + height, winters, dudes) if'-y'in plane: grids.extend (grid ('xz') (U, v), width, height, w, winters, hacks)) if'+ y' in plane: grids.extend (grid ('xz', (u, v), width, height, w + depth, wafers, hacks)) if'-x'in plane: grids.extend ('yz', (v), depth, height, u, dudes) If'+ x'in plane: grids.extend (grid ('yz', (w, v), depth, height, u + width, dudes, hacks)) 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] = 0RGB [RGB [..., 1] = 1] = 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 ()

The above is all the contents of the article "how to draw cubes in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report