Axes3D散点图怎么显示中文
时间: 2024-02-12 14:08:53 浏览: 42
要在 `Axes3D` 散点图中显示中文,需要先下载并安装中文字体,例如 `SimHei` 字体。然后,可以使用以下代码来指定中文字体并显示中文:
```python
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as font_manager
# 指定中文字体
font_path = 'SimHei.ttf'
font_prop = font_manager.FontProperties(fname=font_path, size=14)
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 显示中文标签
ax.set_xlabel('X 轴', fontproperties=font_prop)
ax.set_ylabel('Y 轴', fontproperties=font_prop)
ax.set_zlabel('Z 轴', fontproperties=font_prop)
ax.scatter(x, y, z, c='r', marker='o')
ax.set_title('3D 散点图', fontproperties=font_prop)
plt.show()
```
上述代码中,`font_manager` 库用于加载字体文件,并使用 `FontProperties` 类来指定中文字体和字号。在设置坐标轴标签和标题时,需要使用 `fontproperties` 参数来指定字体。
注意:在使用中文字体时,需要确保字体文件存在,并指定正确的文件路径。