Python绘制3D坐标轴的平行标签

2 投票
2 回答
2351 浏览
提问于 2025-04-18 13:48

我想把3D图上坐标轴的标签从这样:

(标签与坐标轴垂直)

enter image description here

改成这样: (标签与坐标轴平行)

enter image description here 也许,Y标签在这种情况下要旋转90度

我只是用ax.set_xlabel('X axis')来设置每个坐标轴的标签,但结果是标签还是垂直的,而且占用了图表很大一部分空间。

我在看这个讨论,但实际上没有得到答案,而且我不知道get模块是从哪里来的(如果我尝试那个解决方案就会报错)。

2 个回答

2

我猜测你是导入了 axes3d 模块。因为从查看 matplotlib 的示例 来看,axes3d 的默认行为是让标签“垂直”,就像你第一个图那样。但是 Axes3D 模块的默认标签是与坐标轴“平行”的,像你第二个图那样。

至于你提到的讨论,那是针对 matplab 的,不适用于 matplotlib。

这里有一段代码应该能帮到你:

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

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

points = np.random.rand(3, 40)

ax.scatter(points[0], points[1], points[2])

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
0

我找到了一种最好的解决办法:

ax.zaxis.set_rotate_label(False) # To disable automatic label rotation

ax.set_ylabel('Y')
ax.set_xlabel('X', rotation=-90)
ax.set_zlabel('Z', rotation=90)

来自 这里

撰写回答