【问题标题】:using LaTex in matplotlib results in: "Environment align* undefined."在 matplotlib 中使用 LaTex 会导致:\"Environment align* undefined.\"
【发布时间】:2022-11-11 06:59:30
【问题描述】:

我正在尝试在 matplotlib 中使用 align* 环境,遵循此线程

original thread

但是我遇到以下错误:

! LaTeX Error: Environment align undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...               

我假设 someting 不适用于 amsmath 实现,但我不知道究竟是什么。我已经安装了 MikTex,并且正在我的机器上运行 Windows。

这是我的全部代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from decimal import Decimal
import matplotlib as mpl

mpl.rc('text', usetex = True)
mpl.rc('font', **{'family' : "sans-serif"})
params= {'text.latex.preamble' : [r'\usepackage{amsmath}']}
plt.rcParams.update(params)

def lorentz(x, *p):
    y0, xc, w, A = p
    return y0 + (2*A/np.pi) * (w/(4*(x-xc)**2+w**2))

freq = [19.050500, 19.095500, 19.140500, 19.185500, 19.230500, 19.275500, 19.320500, 19.365500, 19.410500, 19.455500, 19.465500, 19.475500, 19.485500, 19.495500, 19.505500, 19.515500, 19.525500, 19.535500, 19.545500, 19.590500, 19.630500, 19.680500, 19.725500, 19.770500, 19.815500, 19.860500, 19.905500, 19.950500]
U = [40, 40, 56, 56, 64, 72, 96, 120, 176, 304, 352, 400, 432, 432, 400, 352, 304, 264, 232, 144, 104, 80, 72, 64, 56, 48, 40, 40]

U_squared = [(item*1e-3)**2 for item in U]

p0 = [2.5e-5, 19.489, 0.0295, 8.3e-4]
coeff, pcov = curve_fit(lorentz, freq, U_squared, p0=p0)

std_err = np.sqrt(np.diag(pcov))

freq_steps = np.linspace(min(freq), max(freq), 1000)

fit = lorentz(freq_steps, *coeff)

fig, ax = plt.subplots()

plt.xlabel(r"$f \, \left[ \mathrm{MHz} \right]$")
plt.ylabel(r"$U^2 \, \left[ \mathrm{V^2} \right]$")

data_plot, = plt.plot(freq, U_squared, linestyle="None", marker=".")
fit_plot, = plt.plot(freq_steps, fit, linestyle="--", color="tab:red")

plt.legend([data_plot, fit_plot],["measured data", "Lorentz fit"])

props = dict(boxstyle='square', facecolor='none', edgecolor='black')

plt.text(0.02, 0.95, r"\begin{align*} U^2 (f) &= y_0 + \frac{2A}{\pi} \frac{w}{4(f-f_0)^2+w^2} \\ " +
         r"y_0 &=\," + "{:.2e}".format(Decimal(coeff[0])) + "\, \pm \," + "{:.2e}".format(Decimal(std_err[0]))
         + r"\\ f_0 &= \," + str(np.round(coeff[1], 2)) + "\, \pm \," + "{:.2e}".format(Decimal(std_err[1]))
         + r"\\ w &= \," + "{:.2e}".format(Decimal(coeff[2])) + "\, \pm \," + "{:.2e}".format(Decimal(std_err[2]))
         + r"\\ A &= \," + "{:.2e}".format(Decimal(coeff[3])) + "\, \pm \," + "{:.2e}".format(Decimal(std_err[3])) + r"\end{align*}",
         transform=ax.transAxes, bbox=props, ha='left')

plt.show()

我感谢每一个帮助!

最好的, 卢卡

【问题讨论】:

    标签: python matplotlib latex


    【解决方案1】:

    params 字典中的值需要是一个字符串(不是字符串列表):

    params= {'text.latex.preamble' : r'usepackage{amsmath}'}
    #instead of
    #params= {'text.latex.preamble' : [r'usepackage{amsmath}']}
    

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2016-02-22
      • 2019-09-26
      • 2018-05-26
      • 2017-01-01
      相关资源
      最近更新 更多