在用f literals编写数学表达式的matplotlob中;如何处理加薪的力量(^)
我想在matplotlob图上显示某些方程式。按照用matplotlob编写数学表达式我写了以下内容。该问题的系数也仅在运行时可用,因此我还使用 python f-string文字。但是它们有些不兼容,因为 {在两种情况下具有不同的含义。这是一个具体示例:
import matplotlib.pyplot as plt
a = 0.3543545
b = 6.834
c = -126.732
s0 = r'$\alpha_i > \beta_i$'
s1 = r"$0.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-string文字时如何用S2或S3与S3复制相同的结果?
I want to display certain equations on a Matplotlob plot. Following 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. But they are somewhat incompatible since { has different meaning in the two cases. Here is a concrete example:
import matplotlib.pyplot as plt
a = 0.3543545
b = 6.834
c = -126.732
s0 = r'$\alpha_i > \beta_i
So how to reproduce the same results with s2 or s3 as s1 when using f-string literals?
s1 = r"$0.354 * x^{6.834} -126.732 quot; # Works as expected
s2 = fr"${a:.3f} * x^{{b:.3f}} {c:+.3f}quot; # Doesn't work; Same as S1 but with f-string literals
s3 = fr"${a:.3f} * x^{b:.3f} {c:+.3f}quot; # 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()
So how to reproduce the same results with s2 or s3 as s1 when using f-string literals?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在F串中,需要由两层表示卷发括号,因此您需要三层来满足您的要求:
In the f-string, curly braces need to be represented by two layers, so you need three layers to meet your requirements: