【发布时间】:2018-11-09 03:26:21
【问题描述】:
我使用 matplotlib。我需要得到 xlabels,如:[1/8,2/8,3/8,4/8..14/8]。我想让它循环起来。因此,为了更好地查看,我在 Python 中使用了 TEX。为了在循环中渲染分数表达式,我使用 .format 方法。但它不能正常工作。在 TEX 中使用 {} 和 .format 方法之间存在一些冲突。我尝试使用双 {},但它也不起作用。
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()
【问题讨论】:
-
{8}到.format()引起了问题。您可以将其留空并将8作为第二个参数传递给format()。如:'$\frac{}{}$'.format(i, 8) -
不幸的是,它不起作用。它会产生错误
ValueError: \frac18 ^ Expected \frac{num}{den}
标签: python matplotlib tex