jupyter python 中文乱码_CentOS 7 安装Jupyter Notebook以及matplotlib画图中文乱码问题

本文介绍了在CentOS 7上安装Jupyter Notebook的过程,包括解决python3导入ssl问题,安装和配置远程访问Jupyter Notebook。此外,还详细说明了matplotlib画图中文乱码的解决方法,通过下载simhei.ttf字体并设置matplotlib字体属性来显示正确中文。
摘要由CSDN通过智能技术生成

一、Linux画图使用ipython比较方便,在此先安装好ipython

1.允许python3使用ssl功能(python3能够import ssl跳过此步)$cd /python/Python-3.6.5/Modules #进入安装目录下的Modules文件夹

$vi Setup

去掉文件中的注释#

然后安装openssl-devel$cd ..

$sudo yum install openssl-devel #yum不能成功时候修改 /usr/libexec/urlgrabber-ext-down和/usr/bin/yum内容,可以参考我博客https://ask.hellobi.com/blog/ysfyb/12780

$sudo yum install bzip2-devel

$make && sudo make install #linux中的虚拟机中未能实现此操,可参考https://ask.hellobi.com/blog/seng/3047,考虑重新安装python

这时候进入python3,能够import ssl就ok

2.安装jupyter$pip install jupyter

$which jupyter-notebook

$/usr/local/bin/jupyter-notebook #jupyter-notebook位置,运行

当python3不能import sqlite3时运行下面语句(jupyter-notebook能够运行跳过此步):$wget https://www.sqlite.org/2017/sqlite-autoconf-3170000.tar.gz --no-check-certificat

$tar zxvf sqlite-autoconf-3170000.tar.gz

$cd sqlite-autoconf-3170000

$./configure --prefix=/usr/local/sqlite3 --disable-static --enable-fts5 --enable-json1 CFLAGS="-g -O2 -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS4=1 -DSQLITE_ENABLE_RTREE=1"

$cd /python/Python-3.6.5/ #python的configure存放位置

$LD_RUN_PATH=/usr/local/sqlite3/lib ./configure LDFLAGS="-L/usr/local/sqlite3/lib" CPPFLAGS="-I /usr/local/sqlite3/include"

$LD_RUN_PATH=/usr/local/sqlite3/lib make &&LD_RUN_PATH=/usr/local/sqlite3/lib sudo make install

3.远程jupyter-notebook

终端中输入jupyter-notebook能够在当地打开本地浏览器访问,而需要远程访问设置如下:$python #进入python设置密码

>>from notebook.auth import passwd

>>passwd() #输入密码后得到生成的密文,复制密文

$jupyter notebook --generate-config #生成默认配置文件

$vi /home/chris/.jupyter/jupyter_notebook_config.py #打开上一步生成的配置文件,修改下面几行内容

>>c.NotebookApp.ip='*'

>>c.NotebookApp.password = 'sha1:52069dc8c86a:f39123e909e46837770fa9f19f7a71ca00243d55'

>>c.NotebookApp.open_browser = False #本地不自动打开浏览器,另外一般默认是8888端口,需要修改在c.NotebookApp.port设置

jupyter notebook多行输出:#找到ipython_config.py添加语句

c = get_config()

c.InteractiveShell.ast_node_interactivity = "all"

后续:升级jupyter后运行jupyter-notebook报错:ValueError: '' does not appear to be an IPv4 or IPv6 address,此时修改文件jupyter_notebook_config.py中c.NotebookApp.ip='0.0.0.0',主要原因是notebook版本不兼容($pip uninstall notebook  $pip install notebook==5.6.0)

二、matplotlib画图乱码问题

1.打开jupyter notebook,在浏览器中画个图import matplotlib.pyplot as plt

plt.plot((1,2,3),(4,3,-1))

plt.xlabel('时间')

plt.ylabel('收入')

plt.show()

2.上图中的中文乱码,这时候需要linux中的找到与matplotlib相关的font文件夹位置$cd / && find -name mpl-data 2>/dev/null

$cd /usr/local/lib/python3.6/site-packages/matplotlib/mpl-data && cd fonts/ttf

3.网上下载simhei.ttf

链接地址:https://fontzone.net/download/simhei,在此文章结尾提供已下载的压缩包,解压后放入ttf文件夹下(可用WinSCP传文件)

4.重新运行程序import matplotlib

import matplotlib.pyplot as plt

plt.plot((1,2,3),(4,3,-1))

myfont = matplotlib.font_manager.FontProperties(fname='/usr/local/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/simhei.ttf')

plt.xlabel('时间', fontproperties=myfont)

plt.ylabel('收入', fontproperties=myfont)

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值