【问题标题】:Using Matplotlib and Numpy, is there a way to find all line intersections for Linear Equations?使用 Matplotlib 和 Numpy,有没有办法找到线性方程的所有线交点?
【发布时间】:2015-01-26 01:16:29
【问题描述】:

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

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

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

【问题讨论】:

    标签: python numpy matplotlib linear-equation


    【解决方案1】:

    如果你只是想画出可行性区域,你可以这样做:

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

    【讨论】:

    • 谢谢Alyase,但我需要知道情节点。我发现了一些迹象,可能类似于此处link 的解释,但我不知道如何使其与我的代码一起使用。
    • 交点很简单,只要计算y1-y2,看看它与0的交点在哪里。
    • 您能否更详细地解释一下这个 sn-p:bottom = np.maximum(y1, 0)
    • @bmc,它返回一个数组,其中包含来自y1 的正值和零,其中y1 为负值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多