Python Pylab - 设置 3d 文本颜色

Python Pylab - set 3d text color

我在 Python Pylab 中有一个 3d 图:

import numpy
import pylab
from PIL import Image, ImageDraw, ImageFont
import mpl_toolkits.mplot3d.axes3d as axes3d

img = Image.new('L', (60, 40), 255)
drw = ImageDraw.Draw(img)
font = ImageFont.truetype('arial.ttf', 20)
drw.text((5, 1), 'TEXT', font = font)

X, Y = numpy.meshgrid(range(60), range(40))
Z = 1 - numpy.asarray(img) / 255

fig = pylab.figure()
ax = axes3d.Axes3D(fig)
ax.plot_surface(X, -Y, Z, rstride = 1, cstride = 1)
ax.set_zlim((0, 50))

fig.show()

我怎样才能使文本(并且只有文本,而不是整个图形)具有特定的颜色?

我试过使用 fillstroke_fill 参数,但它们似乎没有任何作用。有什么建议吗?

您可以通过 plot_surface 命令使用预定义的颜色图(本例中为 viridis)。

ax.plot_surface(X, -Y, Z, rstride = 1, cstride = 1, cmap='viridis')

或者,您也可以创建自己的 colormap,调整 vmax 参数和颜色图的长度以获得所需的输出。

...
...
import mpl_toolkits.mplot3d.axes3d as axes3d

from matplotlib.colors import ListedColormap
colors=["gray", "black"]
cmap = ListedColormap(colors)

img = Image.new('L', (60, 40), 255)
...

ax.plot_surface(X, -Y, Z, rstride = 1, cstride = 1, cmap=cmap, vmin=0, vmax=0.1)
...
...