首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matplotlib中找到函数下面的区域?

如何在matplotlib中找到函数下面的区域?
EN

Stack Overflow用户
提问于 2017-05-16 03:12:06
回答 2查看 4.5K关注 0票数 3

我对python和库matplotlib很陌生,我试图在我的绘图图中把函数线下面的区域。我有一个变量a & b,它在我的绘图中移动一个矩形。我也许可以用原始的数学来解决这个问题,但是我想知道是否有一种更简单的方法来实现我试图用matplotlib做的事情。

我的情节是这样的

我想要得到这个区域的面积

如果有一个简单的方法来给这个区域着色,我也想听听。

下面是显示这个情节的代码:

代码语言:javascript
复制
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()

谢谢你的帮助,很抱歉没有嵌入图像。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-16 03:36:57

可以为您计算积分。,这似乎是获得您想要的东西的最简单的方法。我认为你的照片和你的功能不一致。下面是绘制您的函数复制/粘贴的方法:

代码语言:javascript
复制
t = pd.Series(range(0,80))
plt.plot(-np.cbrt(np.power(t, 2) - 16 * t + 63) +4)

无论如何,这里是如何得到积分,给出一个函数和积分的界限。

代码语言:javascript
复制
import scipy.integrate as integrate

# define components for integral calculation
lower_bound = 22
upper_bound = 36

f = lambda t: -np.cbrt(np.power(t, 2) - 16 * t + 63) +4

# calculate integral
integral, error = integrate.quad(f, lower_bound, upper_bound)
print(integral)

-50.03118191324093

票数 1
EN

Stack Overflow用户

发布于 2017-05-16 03:49:32

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

代码语言:javascript
复制
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()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43991885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档