# 默认目录

jupyter notebook --generate-config
vim /home/zpj/.jupyter/jupyter_notebook_config.py#查找dir设定notebook根目录

找到c.NotebookApp.notebook_dir条目,修改为启动jupyter后打开的目录,如/home/zpj/code

# 配置pylab, matplotlib默认载入

导入库,内联svg输出,需要运行:

%pylab
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

# 配置ipython

# 目的

  • 设置pylab环境自动载入
  • 为notebook设置inline显示,使得图片嵌入在文档中
  • 设置矢量svg格式可以使生成的图像在任何屏幕下获得清晰的显示

# 步骤

创建配置文件

ipython profile create

编辑ipython_config.py

vim ~/.ipython/profile_default/ipython_config.py

修改内容为

c.InteractiveShellApp.pylab = "inline"
c.InlineBackend.figure_formats = ['svg']
c.InteractiveShellApp.pylab_import_all = True

# matplotlib中文

matplotlib似乎只能使用.ttf格式的字体

$ fc-list :lang=zh|grep ttf#产生可用中文字体列表
/usr/share/fonts/truetype/DroidSansFallbackFull.ttf: Droid Sans Fallback:style=Regular

这里设置为Droid Sans Fallback这种常见的字体。用root权限运行以下脚本:

file="/usr/lib64/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc"
if [ -f $file ]
then
  sed -i "s/#*\(font.sans-serif *:\).*/\1 Droid Sans Fallback/g" $file
  sed -i "s/#*\(axes.unicode_minus *:\).*/\1 False/g" $file
  echo "matplotlibrc found and font is configured"
else
  echo "$file not found!"
  echo "Please run locate matplotlibrc to find similiar file locattion"
fi

再重新载入matplotlib即可

In [1]:
plot(arange(10),'o-')
title('吃饭睡觉打豆豆');

# 类似matplotlib.text.Text at xxx的处理

这是由于plot等画图函数的返回值导致的,加分号即可吞掉返回值

# 本地LM Roman字体

参见http://stackoverflow.com/a/42446768 命令:

FROM=/usr/share/javascript/mathjax
NB=/usr/lib/python3.6/site-packages/notebook
TO=$NB/static/components/MathJax
JS=$NB/static/notebook/js/main.min.js
DIR1=fonts/HTML-CSS/TeX
DIR2=jax/output/HTML-CSS/fonts/TeX
ln -s $FROM/$DIR1 $TO/$DIR1
ln -s $FROM/$DIR2 $TO/$DIR2
sed -i 's/\(availableFonts:\).*/\1 \["STIX-Web", "TeX"\],/g' $JS
sed -i 's/\(preferredFonts:\).*/\1 "TeX",/g' $JS
sed -i 's/\(webFont:\).*/\1 "TeX",/g' $JS

最后几句通过sed设置了:

availableFonts: ["STIX-Web","TeX"],
preferredFont: "TeX",
webFont: "TeX",

设置好后刷新即可看到新的字体:$$e^x=\sum_{n=0}^\infty \dfrac{x^n}{n!}$$

# Userstyle for jupyter

Install stylish, and then install Jupyter Serif Font & Code Block Background style.


Comments

comments powered by Disqus