1

如何将字符串添加到 Axes3D 中的轴而不是数字?

我刚开始使用 matplotlib。我使用 Axes3dD 绘制类似于他们网站上给出的示例(http://matplotlib.sourceforge.net/examples/mplot3d/bars3d_demo.html)。请注意,必须使用最后一个版本(matplotlib 0.99.1),否则轴会有点怪异。该示例使用以下代码:

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

fig = plt.figure()  
ax = Axes3D(fig)  
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    xs = np.arange(20)
    ys = np.random.rand(20)
    ax.bar(xs, ys, zs=z, zdir='y', color=c, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

现在我的问题是,我无法将轴重命名为我想要的。我需要将订书钉命名为字符串名称,而不是数字。这我只想在两个维度上做——x 维度和 z 维度(深度)。我试过使用这个命令:

ax.set_xticklabels('First staple',(0,0))

我没有收到错误消息,但仍然没有刺痛。如果有人能回答这个问题,我将非常高兴!

4

2 回答 2

2

你实际上在正确的道路上。但您需要将列表或元组传递给 set_xticklabels(),而不是字符串。您可能还希望使用 set_xticks() 调整标签的中心位置。

您还可以找到使用 get_xmajorticklabels() 的这个函数。它将返回渲染的刻度标签。因此,您也可以根据其结果调整值。

于 2009-09-28T21:20:39.703 回答
0

Also retrieve the axis data, work on that and set it back to the renderer, turned out to be much simpler for me ;) other than that, what NerdyNick says

于 2009-09-28T22:43:30.237 回答