【问题标题】:Printing a Variable using matplotlib's ax.text prints a list of values instead of a number?使用 matplotlib 的 ax.text 打印变量会打印值列表而不是数字?
【发布时间】:2017-08-28 21:39:08
【问题描述】:

我目前正在尝试制作一个程序,该程序将使用 matplotlib 绘制一个函数,绘制它,绘制两个变量之间曲线下的区域,并使用辛普森的 3/8 规则来计算阴影区域。但是,当尝试打印我分配给积分最终值的变量时,它会打印一个列表。

首先,这是我的代码的基础:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

这个定义定义了我将在这里使用的函数,一个简单的多项式。

def func(x):
    return (x - 3) * (x - 5) * (x - 7) + 85

这是计算曲线下面积的函数

def simpson(function, a, b, n):
    """Approximates the definite integral of f from a to b by the
    composite Simpson's rule, using n subintervals (with n even)"""

if n % 2:
    raise ValueError("n must be even (received n=%d)" % n)

h = (b - a) / n #The first section of Simpson's 3/8ths rule
s = function(a) + function(b) #The addition of functions over an interval

for i in range(1, n, 2):
    s += 4 * function(a + i * h)
for i in range(2, n-1, 2):
    s += 2 * function(a + i * h)

return s * h / 3

现在辛普森的规则定义结束了,为了简单起见,我定义了几个变量。

a, b = 2, 9  # integral limits
x = np.linspace(0, 10) #Generates 100 points evenly spaced between 0 and 10
y = func(x) #Just defines y to be f(x) so its ez later on

fig, ax = plt.subplots()
plt.plot(x, y, 'r', linewidth=2)
plt.ylim(ymin=0)

final_integral = simpson(lambda x: y, a, b, 100000)

此时一定有什么问题发生了,但我将包含其余代码,以防您进一步发现问题。

# Make the shaded region
ix = np.linspace(a, b)
iy = func(ix)
verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)]
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
ax.add_patch(poly)

plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
     horizontalalignment='center', fontsize=20)
ax.text(0.25, 135, r"Using Simpson's 3/8ths rule, the area under the curve is: ", fontsize=20)

这里是应该打印积分值的地方:

ax.text(0.25, 114, final_integral , fontsize=20)

以下是绘制图表所需的其余代码:

plt.figtext(0.9, 0.05, '$x$')
plt.figtext(0.1, 0.9, '$y$')

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')

ax.set_xticks((a, b))
ax.set_xticklabels(('$a$', '$b$'))
ax.set_yticks([])

plt.show()

When running this program, you get this graph, and a series of numbers has been printed where the area under the curve should be

感谢您提供任何帮助。我完全被困住了。另外,对不起,如果这有点长,这是我在论坛上的第一个问题。

【问题讨论】:

  • 您的问题确实很长,但它包括所有必要的部分和清晰的问题描述。一个长而完整的问题比一个简短的不完整的问题好。所以为这个好问题提出了建议。

标签: python matplotlib integration anaconda simpsons-rule


【解决方案1】:

您是否尝试过直接为 simpson() 函数提供 func(),而不是使用 lambda 设置?

我认为这可行:

final_integral = simpson(func, a, b, 100000)

你也可以试试:

final_integral = simpson(lambda x: func(x), a, b, 100000)    

发生的事情是 y 是一个具有值 func(x) 的数组,当您使用表达式 lambda x: y 时,您实际上是在创建一个常量函数f(x) = y = const 的形式。然后,您的 final_integral 是一个积分列表,其中每个被积函数都是具有来自 y 数组的特定值的常量函数。

请注意,当您在图表上打印此数字时,您可能需要对其进行格式化,以防它有很多尾随小数点。如何执行此操作取决于您使用的是 Python 2 还是 3

【讨论】:

    【解决方案2】:

    您将 x 分配为 linspace,它是一个数组,因此 y 也是 x 函数的值数组。你可以替换这行代码:

    #old:
    final_integral = simpson(lambda x:y, a, b, 100000)
    
    #new:
    final_integral = simpson(lambda t:func(t), a, b, 100000)
    

    将变量从 x 更改为 t 将为您提供曲线下面积的值。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2022-11-10
      相关资源
      最近更新 更多