plt坐标轴显示中文
时间: 2024-03-31 09:29:28 浏览: 130
在matplotlib中,要在plt坐标轴上显示中文,可以按照以下步骤进行设置:
1. 首先,需要确保你的系统中已经安装了中文字体文件,比如SimHei或者Microsoft YaHei等。如果没有安装,可以通过以下命令安装:
```
sudo apt-get install fonts-wqy-zenhei
```
2. 导入matplotlib库和font_manager模块:
```python
import matplotlib.pyplot as plt
from matplotlib import font_manager
```
3. 使用font_manager模块加载中文字体文件,并设置为默认字体:
```python
font_path = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc' # 中文字体文件路径
font_prop = font_manager.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()
```
4. 在绘图时,使用中文字符即可:
```python
plt.xlabel('横轴', fontproperties=font_prop)
plt.ylabel('纵轴', fontproperties=font_prop)
plt.title('标题', fontproperties=font_prop)
```
这样就可以在plt坐标轴上显示中文了。
阅读全文
相关推荐


















