在用f literals编写数学表达式的matplotlob中;如何处理加薪的力量(^)

发布于 2025-01-24 13:55:48 字数 1093 浏览 0 评论 0原文

我想在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

Plot with equations

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()

Plot with equations

So how to reproduce the same results with s2 or s3 as s1 when using f-string literals?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2025-01-31 13:55:48

在F串中,需要由两层表示卷发括号,因此您需要三层来满足您的要求:

>>> a = 100
>>> f'{{a}}'
'{a}'
>>> f'{{{a}}}'
'{100}'

In the f-string, curly braces need to be represented by two layers, so you need three layers to meet your requirements:

>>> a = 100
>>> f'{{a}}'
'{a}'
>>> f'{{{a}}}'
'{100}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文