使用Matplotlib和Numpy,有没有一种方法可以找到线性方程组的所有线交点?

2024-10-05 14:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码在图上绘制交点,然后直观地检查交点以返回代码并对可行性区域进行着色。在

有没有比简单地画线和从图上读取交点更好的方法来找到可行区域?在

# Filling a polygon locating the corner points
# Then let Matplotlib fill within these points
x= [0.0, 0.0, 6.67,5.0]
y= [0.0, 4.0, .67, 0.0]
fill(x,y)
show()

完整代码示例:

^{pr2}$

Tags: the方法代码区域绘制fill直观points
1条回答
网友
1楼 · 发布于 2024-10-05 14:29:03

如果您只想绘制可行性区域,可以执行以下操作:

x= arange(-3,10.1,0.1)
y= arange(-3,10.1,0.1)
y1= 0.4*x-2.0
y2= 4.0-0.5*x

xlim(-3,10)
ylim(-3,10)
hlines(0,-3,10,color='k')
vlines(0,-3,10,color='k')
grid(True)

xlabel('x-axis')
ylabel('y-axis')
title ('Shaded Area Shows the Feasible Region')

plot(x,y1,color='b')
plot(x,y2,color='r')
legend(['2x-5y=10','x+2y=8'])

bottom = np.maximum(y1, 0)
fill_between(x, bottom, y2, where=(x>0) & (y2>y1))

enter image description here

相关问题 更多 >