How to add text to a surface

Question:

I created rectangular prism with a parallelogram base using plot_surface.
I need to add some text to one of surface. I tried ax.text(3, 0.5, 1, "red", (1, 1, 0), color='red'), but text is not visible over the surface.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(projection='3d')

# Face 1
x1 = np.array([[0, 5, 5, 0, 0],
               [0, 0, 0, 0, 0]])
y1 = np.array([[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0]])
z1 = np.array([[0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0]])
# Face 2
x2 = np.array([[1, 1, 1, 0, 0],
               [0, 0, 0, 0, 0]])
y2 = np.array([[1, 1, 1, 0, 0],
               [0, 0, 0, 0, 0]])
z2 = np.array([[0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0]])
# Face 3
x3 = np.array([[0, 5, 6, 1, 0],
               [0, 0, 0, 0, 0]])
y3 = np.array([[0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0]])
z3 = np.array([[1, 1, 1, 1, 1],
               [1, 1, 1, 1, 1]])
# Face 4
x4 = np.array([[1, 6, 6, 1, 1],
               [1, 1, 1, 1, 1]])
y4 = np.array([[1, 1, 1, 1, 1],
               [1, 1, 1, 1, 1]])
z4 = np.array([[0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0]])
# Face 5
x5 = np.array([[0, 1, 6, 5, 0],
               [0, 0, 0, 0, 0]])
y5 = np.array([[0, 1, 1, 0, 0],
               [0, 0, 0, 0, 0]])
z5 = np.array([[0, 0, 0, 0, 0],
               [0, 0, 0, 0, 0]])
# Face 6
x6 = np.array([[5, 6, 6, 5, 5],
               [1, 1, 1, 1, 0]])
y6 = np.array([[0, 1, 1, 0, 0],
               [0, 0, 0, 0, 0]])
z6 = np.array([[0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0]])


ax.plot_surface(x1,y1,z1)
ax.plot_surface(x2,y2,z2)
ax.plot_surface(x3,y3,z3)
ax.plot_surface(x4,y4,z4)
ax.plot_surface(x5,y5,z5)
ax.plot_surface(x6,y6,z6)

ax.text(3, 0.5, 1, "red", (1, 1, 0), color='red')

plt.show()

How to do it?

enter image description here

Asked By: mswatermelon

||

Answers:

Your surfaces are non-transparent. If you add transparency to all your surfaces you’ll see the text. Try adding ax.plot_surface(x,y,z, alpha=0.5) to see the effect

enter image description here

Answered By: Sleepyhead

You can use zorder:

ax.text(3, 0.5, 1, "red", (1, 1, 0), color='red', zorder=10)

This relates to the relative depth of the different elements (not linked with the z axis)

enter image description here

Answered By: Nicolas Rougier