python - 如何在 matplotlib 中找到函数下方的区域?

标签 python math matplotlib plot

我是 python 和库 matplotlib 的新手,我试图在我的绘图中获取函数线下方的区域。我有一个变量 a & b 可以在我的绘图中移动一个矩形。我可能可以使用原始数学来解决这个问题,但我想知道是否有更简单的方法来实现我想用 matplotlib 做的事情。

我的剧情是这样的

Image

我想得到这个区域的面积

Image

如果,还有一种简单的方法可以给这个区域上色,我也想听听。

这是我显示此图的代码:

plt.clf()
plt.draw()
plt.axis(xmin = 0, xmax = 80, ymin = 0, ymax = 5)
plt.plot(-np.cbrt(np.power(t, 2) - 16 * t + 63) +4)
currentAxis = plt.gca()
line = currentAxis.lines[0]
for x, y in zip(line.get_xdata(), line.get_ydata()):
    if x == 0 or y == 0:
        if b > a:
            currentAxis.add_patch(patches.Rectangle(((a * 80 / 11), y), (b * 80 / 11) - (a * 80 / 11), 5, fill=False))
        else:
            currentAxis.add_patch(patches.Rectangle((-(b * 80 / 11) + 80, y), -(a * 80 / 11) + (b * 80 / 11), 5, fill=False))
plt.show()

感谢您的帮助,抱歉没有嵌入图片。

最佳答案

由于 Max Power 对您问题的集成部分的回答非常好,我将只解决绘制曲线下方/上方区域的问题。您可以使用 fill_between:

import numpy as np
from matplotlib import pyplot as plt
def f(t):
    return -np.cbrt(np.power(t, 2) - 16 * t + 63) +4

t = np.arange(0,80,1/40.)
plt.plot(t,f(t))

section = np.arange(22, 36, 1/20.)
plt.fill_between(section,f(section))

plt.axhline(0, color='k')
plt.axvline(0, color='k')
plt.show()

enter image description here

关于python - 如何在 matplotlib 中找到函数下方的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991885/

相关文章:

python - 我想要绘制一些 Facebook Insights 报告,但日期时间与我的格式不匹配

python - 根据条件合并行

python - 如何在 scikit-learn 中对 SVM 应用标准化?

python - Raspi 3 PIR 传感器 - Python 脚本 - 语法无效

algorithm - 什么更大 : O(mn) OR O((m^2)/n)?

python - matplotlib 绘制所有列的 csv 文件

python - 手动设置图例中点的颜色

python - 指定python模块的开发版本

c - c中范围-5到5的数组的算术平均值

algorithm - 如何以编程方式找到可以逼近另一个黑盒函数的函数?