在 Matplotlib 中用 f-literals 编写数学表达式;如何处理加到幂(^)
In Matplotlob writing mathematical expressions with f-literals; how to deal with the raise to the power (^)
我想在 Matplotlob 图上显示某些方程式。正在关注 Writing mathematical expressions with Matplotlob I wrote the following. The coefficients for the question are also only available at runtime, so I also use python f-string literals。但是它们有些不兼容,因为 { 在这两种情况下具有不同的含义。这是一个具体的例子:
import matplotlib.pyplot as plt
a = 0.3543545
b = 6.834
c = -126.732
s0 = r'$\alpha_i > \beta_i$'
s1 = r"[=10=].354 * x^{6.834} -126.732 $" # Works as expected
s2 = fr"${a:.3f} * x^{{b:.3f}} {c:+.3f}$" # Doesn't work; Same as S1 but with f-string literals
s3 = fr"${a:.3f} * x^{b:.3f} {c:+.3f}$" # Doesn't work;
plt.text(0.1, 0.9, s0)
plt.text(0.1, 0.8, s1)
plt.text(0.1, 0.7, s2)
plt.text(0.1, 0.6, s3)
plt.show()
那么在使用 f 字符串文字时,如何使用 s2 或 s3 重现与 s1 相同的结果?
在f-string中,花括号需要用两层表示,所以需要三层才能满足你的要求:
>>> a = 100
>>> f'{{a}}'
'{a}'
>>> f'{{{a}}}'
'{100}'
我想在 Matplotlob 图上显示某些方程式。正在关注 Writing mathematical expressions with Matplotlob I wrote the following. The coefficients for the question are also only available at runtime, so I also use python f-string literals。但是它们有些不兼容,因为 { 在这两种情况下具有不同的含义。这是一个具体的例子:
import matplotlib.pyplot as plt
a = 0.3543545
b = 6.834
c = -126.732
s0 = r'$\alpha_i > \beta_i$'
s1 = r"[=10=].354 * x^{6.834} -126.732 $" # Works as expected
s2 = fr"${a:.3f} * x^{{b:.3f}} {c:+.3f}$" # Doesn't work; Same as S1 but with f-string literals
s3 = fr"${a:.3f} * x^{b:.3f} {c:+.3f}$" # Doesn't work;
plt.text(0.1, 0.9, s0)
plt.text(0.1, 0.8, s1)
plt.text(0.1, 0.7, s2)
plt.text(0.1, 0.6, s3)
plt.show()
那么在使用 f 字符串文字时,如何使用 s2 或 s3 重现与 s1 相同的结果?
在f-string中,花括号需要用两层表示,所以需要三层才能满足你的要求:
>>> a = 100
>>> f'{{a}}'
'{a}'
>>> f'{{{a}}}'
'{100}'