小编Ser*_*lin的帖子

在 matplotlib python 中使用带 tex 分数表达式的格式

我使用 matplotlib。我需要获得 xlabel,例如:[1/8,2/8,3/8,4/8..14/8]。我想把它做成一个循环。因此,为了更好地查看,我在 Python 中使用 TEX。为了在循环中渲染分数表达式,我使用 .format 方法。但它不能正常工作。在 TEX 中使用 {} 和 .format 方法之间存在一些冲突。我尝试使用 double {} 但它也不起作用。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
xax = ax.xaxis  # get x axis

labels = ['0']
for i in range(0, 14):
    labels.append(r'$\frac{}{8}$'.format(i)) # when trying to insert i an error occurs

xax.set_ticklabels(labels)

plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib tex

4
推荐指数
1
解决办法
3177
查看次数

标签 统计

matplotlib ×1

python ×1

tex ×1