jupyter notebook不能显示中文文件
时间: 2023-12-03 13:05:01 浏览: 718
解决jupyter notebook不能显示中文文件的问题,可以按照以下两种方法进行操作:
1. 修改jupyter notebook配置文件
在终端中输入以下命令打开jupyter notebook配置文件:
```shell
jupyter notebook --generate-config
```
找到并修改配置文件中的以下两行代码:
```shell
#c.NotebookApp.locale = 'en_US'
#c.UTF8Encoding = False
```
将其修改为:
```shell
c.NotebookApp.locale = 'zh_CN.UTF-8'
c.UTF8Encoding = True
```
保存并退出配置文件,重新启动jupyter notebook即可。
2. 安装中文字体
在终端中输入以下命令安装中文字体:
```shell
sudo apt-get install fonts-wqy-zenhei
```
安装完成后,重新启动jupyter notebook即可。
相关问题
jupyter notebook中文界面显示
### 配置 Jupyter Notebook 显示中文界面
为了使 Jupyter Notebook 显示中文界面,可以按照如下方法操作:
修改 Jupyter Notebook 的配置文件是实现这一目标的关键步骤之一。在命令行中输入以下命令来生成或访问现有的配置文件[^1]。
```bash
jupyter notebook --generate-config
```
此命令会在用户的 Jupyter 文件夹内创建一个名为 `jupyter_notebook_config.py` 的配置文件。对于 Windows 用户,默认情况下该路径可能是类似于 `%APPDATA%\jupyter\jupyter_notebook_config.py` 或者 `.jupyter/jupyter_notebook_config.py` 对于 Linux 和 macOS 用户而言[^2]。
之后,在浏览器中打开 Jupyter Notebook 后,如果界面上仍然显示为英文版本,并且希望将其更改为中文,则可以通过安装特定的语言包或者扩展插件来完成这个过程。一种常见的做法是在 Anaconda 环境里通过 conda 安装 `nbextension` 工具集以及其中包含的翻译支持功能[^4]。
另外需要注意的是,某些新版本可能取消了一些传统的 UI 特征比如 "In/Out" 提示符;因此,当遇到这种情况时考虑适当调整使用的 Jupyter Notebook 版本也可能会有所帮助。
最后值得注意的一点是,PyCharm 中集成的 Jupyter Notebook 可能会有一些不同的设置方式,但这不影响上述关于更改语言环境的操作指南适用性[^3]。
Jupyter notebook画图中文显示为方框
### Jupyter Notebook 中 Matplotlib 绘制图形时中文显示为方框的解决方案
在 Jupyter Notebook 使用 Matplotlib 进行绘图时遇到中文字符显示成方框的问题,通常是由于缺少合适的中文字体或未正确设置字体配置所致。
为了使中文能够正常显示,可以采取以下措施:
通过 Python 代码动态调整 Matplotlib 的默认参数来指定支持中文的字体。这可以通过修改 `matplotlib.rcParams` 来实现[^3]。具体来说,需要确保设置了适合处理中文文本的字体系列以及适当处理特殊符号如负号的情况。以下是具体的代码片段用于设置这些属性:
```python
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 设置黑体作为缺省字体
plt.rcParams['axes.unicode_minus']=False # 正常化负号显示
```
另外一种方法是从根本上解决问题,即编辑位于特定路径下的全局配置文件 `matplotlibrc` 。对于不同操作系统而言,该文件的位置可能有所不同。例如,在 Linux 上可能是类似于 `/miniconda2/lib/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc` 的位置[^4];而在 macOS 下,则可以在 Finder 菜单中的“前往”选项卡下找到“访问目录”,然后定位至相应文件夹[^5]。一旦找到了此文件,就需要向其中添加如下两行内容以指明要使用的字体家族,并保存更改后的文件:
```
font.family: sans-serif
font.sans-serif: SimHei, DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
```
最后一步是确认已下载并安装了所选字体(比如思源黑体),并且将其放置于系统的字体库内以便 Matplotlib 可以识别和应用它。如果是在 Windows 平台上操作的话,可以直接把 `.ttf` 文件放入 C:\Windows\Fonts 目录下;如果是 Mac 或者其他 Unix 类型的操作系统,则应按照各自平台的方式来进行字体安装。
完成上述任一方式之后再次尝试绘制含有中文标签或其他元素的图表应该就不会再看到那些恼人的空白方格了。
阅读全文
相关推荐














