[英]How to add text on surfaces of cubes
How to add text on surfaces of cubes.如何在立方体的表面上添加文本。 I'm trying to solve 3d packing problem but i have problem visualization because if there were 1000 cubes, how to identify each of them.So i need to write number on surfaces(every surfaces if it is possible).
我正在尝试解决 3d 包装问题,但我有可视化问题,因为如果有 1000 个立方体,如何识别它们中的每一个。所以我需要在表面上写数字(如果可能的话,每个表面)。
output that i dont want:我不想要的 output:
output that i need:我需要的 output:
Code:代码:
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
import matplotlib.pyplot as plt
def cuboid_data2(o, size=(1,1,1)):
X = [[[0, 1, 0], [0, 0, 0], [1, 0, 0], [1, 1, 0]],
[[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0]],
[[1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1]],
[[0, 0, 1], [0, 0, 0], [0, 1, 0], [0, 1, 1]],
[[0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]],
[[0, 1, 1], [0, 0, 1], [1, 0, 1], [1, 1, 1]]]
X = np.array(X).astype(float)
for i in range(3):
X[:,:,i] *= size[i]
X += np.array(o)
return X
def plotCubeAt2(positions,sizes=None,colors=None, **kwargs):
if not isinstance(colors,(list,np.ndarray)): colors=["C0"]*len(positions)
if not isinstance(sizes,(list,np.ndarray)): sizes=[(1,1,1)]*len(positions)
g = []
for p,s,c in zip(positions,sizes,colors):
g.append( cuboid_data2(p, size=s) )
return Poly3DCollection(np.concatenate(g),
facecolors=np.repeat(colors,6), **kwargs)
positions = [(-3,5,-2),(1,7,1)]
sizes = [(4,5,3), (3,3,7)]
colors = ["lightblue","pink"]
fig = plt.figure()
ax = fig.gca(projection='3d')
# ax.set_aspect('equal')
pc = plotCubeAt2(positions,sizes,colors=colors, edgecolor="k")
ax.add_collection3d(pc)
ax.set_xlim([-4,6])
ax.set_ylim([4,13])
ax.set_zlim([-3,9])
plt.show() ```
You can add text
to 3D-Axes specifying the position and direction.您可以将
text
添加到指定 position 和方向的 3D 轴。 The following example puts the text on the center of the frontal xz face of each box:以下示例将文本放在每个框的正面 xz 面的中心:
xz_sizes = np.array(sizes)
xz_sizes[:,1] = 0
label_pos = (np.array(positions) + xz_sizes / 2).tolist()
labels = ['12', '24']
for pos, label in zip(label_pos, labels):
ax.text( *pos, label, 'x', ha='center', va='center')
PS: if you like you can directly calculate label_pos
as a one-liner but for me this seems to be more convoluted than using the auxiliary array xz_sizes
: PS:如果您愿意,可以直接将
label_pos
计算为单线,但对我而言,这似乎比使用辅助数组xz_sizes
更复杂:
label_pos = (np.array(positions) + np.insert(np.array(sizes)[:, [0,2]], 1, 0, axis=1) / 2).tolist()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.