How to add text on surfaces of cubes

Question:

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

output that i dont want:

output that i dont want

output that i need:

output that i need

Asked By: LogicLover90

||

Answers:

You can add text to 3D-Axes specifying the position and direction. The following example puts the text on the center of the frontal x-z face of each box:

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

enter image description here

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:

label_pos = (np.array(positions) + np.insert(np.array(sizes)[:, [0,2]], 1, 0, axis=1) / 2).tolist()

Update: putting the labels on all surfaces works exactly the same way: the following examples show it for two other surfaces (1 x front, 1 x back), so I guess you get the idea:

label_pos_y = (np.array(positions) + np.insert(np.array(sizes)[:, [0,2]] / 2, 1, 0, axis=1)).tolist()
label_pos_x = (np.array(positions) + np.insert(np.array(sizes)[:, [1,2]] / 2, 0, 0, axis=1)).tolist()
label_pos_z = (np.array(positions) + np.insert(np.array(sizes)[:, [0,1]] / 2, 2, np.array(sizes)[:,2], axis=1)).tolist()

labels = ['12', '24']
for pos_y, pos_x, pos_z, label in zip(label_pos_y, label_pos_x, label_pos_z, labels):
    ax.text( *pos_y, label, 'x', ha='center', va='center')
    ax.text( *pos_x, label, 'y', ha='center', va='center')
    ax.text( *pos_z, label, 'x', ha='center', va='center')

enter image description here

Answered By: Stef
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.