首页 > 解决方案 > 如何在 jupyter notebook 中使用非衬线字体制作 matplotlib 绘图?

问题描述

我想在非衬线字体的 jupyter 笔记本中使用乳胶绘图。

我安装了一些软件包:

sudo apt install font-manager texlive-latex-base texlive-latex-extra texlive-math-extra dvipng

我清除了 matplotlib 缓存:

rm ~/.cache/matplotlib -rf

我正在使用这段代码:

%pylab inline
rcParams['text.usetex'] = True
rcParams['text.latex.unicode'] = True
rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']
plot(range(10))
title('Test')
xlabel('Test')

虽然情节有衬线刻度标签:

在此处输入图像描述

标签: matplotliblatexjupyter-notebook

解决方案


使用该功能激活乳胶后rc,使用以下乳胶前导似乎是获得所需结果所必需的。这有效,但我不知道为什么。我猜%pylab inline有魔法吗?你甚至不必告诉matplotlib使用这样的无衬线字体。\mathrm使标题中的公式变为非斜体。

%pylab inline
figure(dpi=100)
rc('text', usetex=True)
rcParams['text.latex.preamble'] = [r'\usepackage[cm]{sfmath}']
plot(np.arange(10)/10)
xlabel(r'Time $i_{k} [s]$', size=12)
title('$\mathrm{\sum_{i} T_i}$')

在此处输入图像描述


推荐阅读