matplotlib 数学文本斜体分数

1 投票
1 回答
1516 浏览
提问于 2025-04-18 18:23

请问用matplotlib能不能创建一个 斜体分数(在LaTeX中是/sfrac{1}{1})呢?

我试过 r'$/sfrac{1}{2}$',但是没有任何效果...

1 个回答

0

你需要使用真正的LaTeX,像下面这样:

import matplotlib
import matplotlib.pyplot as plt

# Use LaTeX for rendering
matplotlib.rcParams["text.usetex"] = True
# load the xfrac package
matplotlib.rcParams["text.latex.preamble"].append(r'\usepackage{xfrac}')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([0,1],[1,0])
ax.text(.5, .5, r'$\sfrac{1}{2}$')

这样会生成:

在这里输入图片描述

你得确保你的系统里有可以用的LaTeX,当然还需要安装LaTeX的xfrac这个包。

撰写回答