python jupyter matplotlib画图显示中文
时间: 2024-03-14 10:42:11 浏览: 147
在Python Jupyter中使用Matplotlib绘制图形并显示中文,可以按照以下步骤进行设置:
1. 安装字体库:首先需要安装支持中文的字体库,比如SimHei或者Microsoft YaHei。可以通过以下命令安装字体库:
```
!apt-get install -y fonts-wqy-zenhei
```
2. 导入必要的库:在Jupyter Notebook中导入所需的库,包括Matplotlib和FontProperties。
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
```
3. 设置字体:选择合适的体,并将其设置为Matplotlib的默认字体。
```
font = FontProperties(fname='/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc')
plt.rcParams['font.family'] = font.get_name()
```
4. 绘制图形:使用Matplotlib绘制图形,并在需要显示中文的地方使用`fontproperties`参数指定字体。
```python
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('横轴', fontproperties=font)
plt.ylabel('纵轴', fontproperties=font)
plt.title('示例图', fontproperties=font)
plt.show()
```
这样就可以在Jupyter Notebook中绘制图形并显示中文了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)