plt.show()显示图像中文乱码
时间: 2023-06-12 12:05:36 浏览: 58
这个问题通常是因为 matplotlib 默认使用系统字体,而系统字体中没有包含中文字符集。
解决方法是在代码中指定使用中文字体,例如:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/path/to/your/font.ttf') # 指定中文字体文件路径
plt.rcParams['font.family'] = font.get_name()
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
```
其中 `'/path/to/your/font.ttf'` 需要替换成你安装的中文字体文件的路径。如果你没有安装中文字体,可以从网上下载一些常用的中文字体文件,例如 SimSun.ttf、SimHei.ttf 等。
如果你使用的是 Jupyter Notebook,可以在开头添加以下代码来全局设置中文字体:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname='/path/to/your/font.ttf') # 指定中文字体文件路径
plt.rcParams['font.family'] = font.get_name()
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
%matplotlib inline # 在 Jupyter Notebook 中显示图像
```