Matplotlib。向表面添加文本

时间:2014-05-22 18:39:36

标签: python matplotlib

我使用plot_surface创建了一个带有平行四边形底座的矩形棱镜。 我需要在表面之一添加一些文字。我尝试了ax.text(3, 0.5, 1, "red", (1, 1, 0), color='red'),但表面上看不到文字。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
ax = Axes3D(fig)

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

plt.show()

怎么做?

2 个答案:

答案 0 :(得分:0)

您的表面不透明。如果您为所有曲面添加透明度,则会看到文本。尝试添加ax.plot_surface(x,y,z, alpha=0.5)以查看效果

答案 1 :(得分:0)

您可以使用zorder:

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

这涉及不同元素的相对深度(不与z轴连接)