一、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()