传奇的格式用Matplotlib中的方程式

发布于 2025-01-27 00:54:14 字数 388 浏览 1 评论 0原文

我们如何在Pyplot图的传说中显示一个方程式? 下面的示例代码:

fig = plt.figure(figsize=(5, 3), dpi=my_dpi)
ax = fig.add_subplot(111)

a = 2.0
b = 3.0

xdata = np.arange(6)
ydata = a*np.power(xdata,b)

# I want the b variable in the equation below to be a subscript.
label = '${0:.2f} x^{{0:.2f}}$'.format(a,b)

plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()

How do we show an equation in the legend of a pyplot plot?
Sample code below:

fig = plt.figure(figsize=(5, 3), dpi=my_dpi)
ax = fig.add_subplot(111)

a = 2.0
b = 3.0

xdata = np.arange(6)
ydata = a*np.power(xdata,b)

# I want the b variable in the equation below to be a subscript.
label = '${0:.2f} x^{{0:.2f}}
.format(a,b)

plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()

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

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

发布评论

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

评论(1

动听の歌 2025-02-03 00:54:14

一种可能的解决方案:首先将浮子格式化为字符串,而不是将字符串放在一起:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5, 3))
ax = fig.add_subplot(111)

a = 2.0
b = 3.0

xdata = np.arange(6)
ydata = a*np.power(xdata,b)

f2str = '{:.2f}'
label = '$%s x^{%s}
 % tuple(f2str.format(t) for t in [a, b])

plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()

One possible solution: first format your float to strings, than put the strings together:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5, 3))
ax = fig.add_subplot(111)

a = 2.0
b = 3.0

xdata = np.arange(6)
ydata = a*np.power(xdata,b)

f2str = '{:.2f}'
label = '$%s x^{%s}
 % tuple(f2str.format(t) for t in [a, b])

plt.plot(xdata,ydata, alpha = 0.5, label = label)
plt.legend()
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文