python - 使用 Matplotlib 和 Numpy,有没有办法找到线性方程的所有直线交点?

标签 python numpy matplotlib linear-equation

我正在使用以下代码在图表上绘制交点,然后目视检查交点以返回代码并对可行性区域进行着色。

有没有比简单地绘制直线并从图上读取交点更好的方法来找到可行区域?

# 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()

完整代码示例:

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'])

x= [0.0, 0.0, 6.67,5.0]
y= [0.0, 4.0, .67, 0.0]
fill(x,y)
show()

最佳答案

如果您只想绘制可行性区域,您可以这样做:

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

关于python - 使用 Matplotlib 和 Numpy,有没有办法找到线性方程的所有直线交点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143546/

相关文章:

python - Matplotlib/ Pandas : Zoom Part of a Plot with Time Series

python - 如何在 Matplotlib 中使用不同主网格间距的 xticklabel?

python - 如何使 python .post() 请求重试?

python - 在Python(最好是py3k)上使用sscanf?

python - 使用 django haystack 查询第二个模型

python - 在 numpy 和 python 中对图像进行简单、高效的双线性插值

python - 使用 Spyder/Python 打开 .npy 文件

极坐标图多边形的Python/Matplotlib反填充

python - Numpy 特征向量计算不正确

python - 情节 : Too many ticks on X axe