【问题标题】:How do you include a bold symbol in the axis when exporting a pgf figure using matplotlib?使用 matplotlib 导出 pgf 图形时,如何在轴中包含粗体符号?
【发布时间】:2022-01-08 06:50:05
【问题描述】:

我在将使用 matplotlib 制作的绘图导出到 pgf 时遇到问题。没有轴标签中的粗体符号,没有问题,但我得到一个错误。

这是我的代码:

import seaborn as sns
import numpy as np
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
mpl.use('pgf')
mpl.rcParams.update({
    'pgf.texsystem': "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'font.serif': ["Computer Modern Roman"],
})
mpl.rc('text.latex', preamble=r'\usepackage{bm}')
import matplotlib.font_manager
import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize = (8,4))
sns.set_color_codes("colorblind")
b = sns.barplot(x=probs, y=words, color='b')
ax.set(xlabel = r'{Probability of word, $p(w|\boldsymbol{\pi}_{ML})$}',
    ylabel = r'{Word, $w$}' )
b.set_yticklabels(labels=b.get_yticklabels(), va='center')
f.tight_layout()
plt.savefig('graph.pgf', format='pgf')

我得到的错误是:

LatexError: LaTeX process halted
! Undefined control sequence.
<argument> ...rd, \(\displaystyle p(w|\boldsymbol 
                                                  {\pi }_{ML})\)}
<*> ...displaystyle p(w|\boldsymbol{\pi}_{ML})\)}}
                                                  
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on texput.log.

我认为在我的序言中包含\usepackage{bm} 会规避这个问题?

任何帮助将不胜感激!

【问题讨论】:

  • 你能显示生成的 pgf 代码吗?

标签: python matplotlib latex axis-labels pgf


【解决方案1】:

本周第二次......尝试调试和搜索数小时后,我在 stackoverflow 上发布后立即找到了解决方案。

解决方案:

mpl.rcParams.update({
    'pgf.texsystem': "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    "pgf.preamble": "\n".join([
         r"\usepackage{bm}",
    ]),
    'font.serif': ["Computer Modern Roman"],
    'text.latex.preamble': pream,
})

【讨论】:

    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2017-11-10
    相关资源
    最近更新 更多