首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在matplotlib中填充行间区域

如何在matplotlib中填充行间区域
EN

Stack Overflow用户
提问于 2019-04-18 15:10:28
回答 2查看 1.4K关注 0票数 1

在matplotlib中尝试了所有可能的绘制后,我想从下面的方程式中填充最大化的区域,但无法填充所需的区域。

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt

A = np.linspace(0, 100, 2000)

# 3A+4B≤30

y1 = (30 - A * 3 ) /4
# 5A+6B≤60
y2 = (60 - A * 5)/6
# 1.5A+3B≤21
y3 = (21 - A * 1.5)/3.0

plt.plot(A, y1, label=r'$3A+4B\leq30$')
plt.plot(A, y2, label=r'$5A+6B\leq60$')
plt.plot(A, y3, label=r'$1.5A+3B\leq21$')


plt.xlim((0, 20))
plt.ylim((0, 15))
plt.xlabel(r'$x values$')
plt.ylabel(r'$y values$')

plt.fill_between(A, y3, where = y2<y3,color='grey', alpha=0.5)
plt.legend(bbox_to_anchor=(.80, 1), loc=2, borderaxespad=0.1)
plt.show()

想要填充x= 2.0和y= 6.0的maxim面积

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-18 19:46:43

这是一种基于this链接的解决方案。与链接解决方案的唯一区别是,对于您的情况,我必须使用fill_betweenx来覆盖曲线共有的整个x轴,并切换xY的顺序。其思想是首先找到某个公差内的交点,然后从左侧的一条曲线和右侧的另一条曲线获取交叉点的值。我还必须在ind中添加一个额外的[0]才能使其正常工作

代码语言:javascript
运行
复制
import numpy as np
import matplotlib.pyplot as plt

A = np.linspace(0, 100, 2000)

y1 = (30 - A * 3 ) /4
y2 = (60 - A * 5)/6
y3 = (21 - A * 1.5)/3.0

plt.plot(A, y1, label=r'$3A+4B\leq30$')
plt.plot(A, y2, label=r'$5A+6B\leq60$')
plt.plot(A, y3, label=r'$1.5A+3B\leq21$')

plt.xlim((0, 20))
plt.ylim((0, 12))
plt.xlabel(r'$x values$')
plt.ylabel(r'$y values$')

plt.legend(bbox_to_anchor=(.65, 0.95), loc=2, borderaxespad=0.1)

def fill_below_intersection(x, S, Z):
    """
    fill the region below the intersection of S and Z
    """
    #find the intersection point
    ind = np.nonzero( np.absolute(S-Z)==min(np.absolute(S-Z)))[0][0]
    # compute a new curve which we will fill below
    Y = np.zeros(S.shape)
    Y[:ind] = S[:ind]  # Y is S up to the intersection
    Y[ind:] = Z[ind:]  # and Z beyond it
    plt.fill_betweenx(Y, x, facecolor='gray', alpha=0.5) # <--- Important line

fill_below_intersection(A, y3, y1)

票数 2
EN

Stack Overflow用户

发布于 2019-04-18 15:30:20

我假设您希望填充y1y3之间的区域,直到它们彼此相交,因为您指定(2,6)作为点?然后使用:

代码语言:javascript
运行
复制
plt.fill_between(A, y1, y3, where = y1<y3)

如果您指的是另一条曲线,请类比地将y2替换为y3。正如@gmds已经评论的那样,“最大化面积”有点误导。

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

https://stackoverflow.com/questions/55740846

复制
相关文章

相似问题

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