【问题标题】:I am trying to draw feasible region of these constraints, but I am stuck我正在尝试绘制这些约束的可行区域,但我被卡住了
【发布时间】:2022-10-12 22:04:33
【问题描述】:

我的约束是 x_1 + x_2 <= 120, x_1 + 0.5x_2 <= 65, 15 <= x_1 <= 80, x_1 >= 0 和 x_2 >= 0

x_1 = np.linespace(0,30,1000)
x_2 = np.linespace(0,30,1000)

#plot
fig, ax = plt.subplots()
fig.set_size_inches(14.7, 8.27)

# draw constraints
plt.axvline(80, color='g', label=r'$x_1 \geq 80$') # constraint 1
plt.axvline(15, color='g', label=r'$x_1 \geq 15$') # constraint 2
plt.plot(x_1, (120 - x_1) / 3) # constraint 3
plt.plot(x_1, (65 - x_1) / 0.5) # constraint 4
plt.xlim((0, 120))
plt.ylim((0,130))
plt.ylabel(r'x1')
plt.ylabel(r'x2')

# fill in the fesaible region
plt.fill_between(x_1, np.minimum((65 - x_1) / 0.5, (120 - x_1) / 3), np.minimum((65 - x_1) / 0.5, 0),
x_1 >= 15,
color='green', alpha=0.25)
plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.)

# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()

从图中可以看出,线不接触x轴

【问题讨论】:

    标签: python matplotlib visualization


    【解决方案1】:

    您的行没有到达 x 轴,因为 x_1x_2 的上限是 30:x_1 = np.linspace(0,30,1000)

    您可以扩展范围并设置plt.ylim(bottom=0) 以将可见区域限制为x_2&gt;0

    x_1 = np.linspace(0,80,1000)
    x_2 = np.linspace(0,80,1000)
    
    #plot
    fig, ax = plt.subplots()
    fig.set_size_inches(14.7, 8.27)
    
    # draw constraints
    plt.axvline(80, color='g', label=r'$x_1 geq 80$') # constraint 1
    plt.axvline(15, color='g', label=r'$x_1 geq 15$') # constraint 2
    plt.plot(x_1, (120 - x_1) / 3) # constraint 3
    plt.plot(x_1, (65 - x_1) / 0.5) # constraint 4
    plt.xlim((0, 120))
    plt.ylabel(r'x1')
    plt.ylabel(r'x2')
    
    # fill in the fesaible region
    plt.fill_between(x_1, np.minimum((65 - x_1) / 0.5, (120 - x_1) / 3), np.minimum((65 - x_1) / 0.5, 0),
    x_1 >= 15,
    color='green', alpha=0.25)
    plt.ylim(bottom=0)
    plt.legend(bbox_to_anchor=(1, 1), loc=1, borderaxespad=0.)
    
    # Hide the right and top spines
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    plt.show()
    

    这是你想要的?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多