python - matplotlib,在图例标签上包含多个已定义变量

标签 python matplotlib

我试图将两个或多个先前在代码中定义的变量包含到图例的标签或绘制文本时。例如,我绘制了一堆函数,我想要图例上每个函数的方程式。如果我只有像 y = Ax 这样的东西,其中 A 是通过将函数拟合到某些数据而找到的,我会写这样的东西:

 plt.plot(x, y, label='y = %.2f x' %A) 

在图例中,我会看到等式中 A 的实际值。现在,如果我想对 y = Ax + B 做同样的事情,我就去

 plt.plot(x, y, label='y = %.2f x + %.2f' %A %B)

我得到一个错误。我尝试过的大多数方法都会给我语法错误,我得到的方法是格式字符串的参数不足。

最佳答案

试试这个

 plt.plot(x, y, label='y = %.2f x + %.2f' %(A, B))

关于python - matplotlib,在图例标签上包含多个已定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796771/

相关文章:

python - 返回条件语句

python - 使用插值和 alpha 将 imshow 保存为 pdf 时出现 Matplotlib 问题

python - 区分 `colorbar` 中的截取值

python - matplotlib 中绘图内的刻度标签

python - 将文本文件中的列存储在数组中

python - Linux 上的 OpenCv + Gstreamer + python

python - 使用 pandas 数据框按索引进行移位

Python 正则表达式 :combining re pattern format with a variable

python - 使用概率分布扩展整数的随机范围

python - 如何在 matplotlib 中获取图例位置