Skip to content

[lp_intro] change the visualization code #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
longye-tian opened this issue Jun 26, 2024 · 4 comments
Closed

[lp_intro] change the visualization code #475

longye-tian opened this issue Jun 26, 2024 · 4 comments

Comments

@longye-tian
Copy link
Collaborator

Dear John @jstac ,

This issue tracker is related to #469 about change the visualization code.

The current visualization code is

fig, ax = plt.subplots()
ax.grid()

# Draw constraint lines
ax.hlines(0, -1, 17.5)
ax.vlines(0, -1, 12)
ax.plot(np.linspace(-1, 17.5, 100), 6-0.4*np.linspace(-1, 17.5, 100), color="r")
ax.plot(np.linspace(-1, 5.5, 100), 10-2*np.linspace(-1, 5.5, 100), color="r")
ax.text(1.5, 8, "$2x_1 + 5x_2 \leq 30$", size=12)
ax.text(10, 2.5, "$4x_1 + 2x_2 \leq 20$", size=12)
ax.text(-2, 2, "$x_2 \geq 0$", size=12)
ax.text(2.5, -0.7, "$x_1 \geq 0$", size=12)

# Draw the feasible region
feasible_set = Polygon(np.array([[0, 0],
                                 [0, 6],
                                 [2.5, 5],
                                 [5, 0]]),
                       color="cyan")
ax.add_patch(feasible_set)

# Draw the objective function
ax.plot(np.linspace(-1, 5.5, 100), 3.875-0.75*np.linspace(-1, 5.5, 100), color="orange")
ax.plot(np.linspace(-1, 5.5, 100), 5.375-0.75*np.linspace(-1, 5.5, 100), color="orange")
ax.plot(np.linspace(-1, 5.5, 100), 6.875-0.75*np.linspace(-1, 5.5, 100), color="orange")
ax.arrow(-1.6, 5, 0, 2, width = 0.05, head_width=0.2, head_length=0.5, color="orange")
ax.text(5.7, 1, "$z = 3x_1 + 4x_2$", size=12)

# Draw the optimal solution
ax.plot(2.5, 5, "*", color="black")
ax.text(2.7, 5.2, "Optimal Solution", size=12)

plt.show()

After our meeting today, I propose to remove the grid, set x1 = np.linspace(0,15) and use x1 in the plot code, add legend instead of text and add x,y labels, remove unnecessary color specifications (I add the orange color to the iso-revenue lines so that they are in the same color).

My code is shown below:

fig, ax = plt.subplots()
# Draw constraint lines
ax.set_xlim(0,15)
ax.set_ylim(0,10)
x1 = np.linspace(0, 15)
ax.plot(x1, 6-0.4*x1, label="$2x_1 + 5x_2=30$")
ax.plot(x1, 10-2*x1, label="$4x_1 + 2x_2=20$")


# Draw the feasible region
feasible_set = Polygon(np.array([[0, 0],[0, 6],[2.5, 5],[5, 0]]))
ax.add_patch(feasible_set)

# Draw the objective function
ax.plot(x1, 3.875-0.75*x1, label="iso-revenue lines",color='orange')
ax.plot(x1, 5.375-0.75*x1, color='orange')
ax.plot(x1, 6.875-0.75*x1, color='orange')

# Draw the optimal solution
ax.plot(2.5, 5, "*", label="optimal solution")
ax.set_xlabel("$x_1$")
ax.set_ylabel("$x_2$")
ax.legend()

plt.show()

The current and proposed outputs are shown below:
current plot
new plot

What do you think about the proposed changes in the visualization code? Would you like to make any changes?

Best ❤️
Longye

@jstac
Copy link
Contributor

jstac commented Jun 27, 2024

Many thanks @longye-tian . I like your code and figure a lot better.

Can you please increase alpha in the shading of the feasible set, so that the color is more transparent? Then it will be a different color to the blue line. Also, can you please change the iso-revenue lines to black, maybe half the default width, or with alpha=0.6, so they are not too dominant.

Finally, can you please change the star at the optimal solution to a dot? Simple is best.

@longye-tian
Copy link
Collaborator Author

Many thanks @longye-tian . I like your code and figure a lot better.

Can you please increase alpha in the shading of the feasible set, so that the color is more transparent? Then it will be a different color to the blue line. Also, can you please change the iso-revenue lines to black, maybe half the default width, or with alpha=0.6, so they are not too dominant.

Finally, can you please change the star at the optimal solution to a dot? Simple is best.

Dear John @jstac ,

Sure thing. Just revised the code accordingly. The new code

  • set alpha=0.1 for the feasible set
  • set the iso-revenue lines to black with half the default linewidth linewidth=0.75
  • change the star to dot at the optimal solution
fig, ax = plt.subplots()
# Draw constraint lines
ax.set_xlim(0,15)
ax.set_ylim(0,10)
x1 = np.linspace(0, 15)
ax.plot(x1, 6-0.4*x1, label="$2x_1 + 5x_2=30$")
ax.plot(x1, 10-2*x1, label="$4x_1 + 2x_2=20$")


# Draw the feasible region
feasible_set = Polygon(np.array([[0, 0],[0, 6],[2.5, 5],[5, 0]]), alpha=0.1)
ax.add_patch(feasible_set)

# Draw the objective function
ax.plot(x1, 3.875-0.75*x1, label="iso-revenue lines",color='k',linewidth=0.75)
ax.plot(x1, 5.375-0.75*x1, color='k',linewidth=0.75)
ax.plot(x1, 6.875-0.75*x1, color='k',linewidth=0.75)

# Draw the optimal solution
ax.plot(2.5, 5, ".", label="optimal solution")
ax.set_xlabel("$x_1$")
ax.set_ylabel("$x_2$")
ax.legend()

plt.show()

The output is shown below:
revised plot

This indeed looks much better!

If you like this output, I will create a pull request using the above code.

Best ❤️
Longye

@jstac
Copy link
Contributor

jstac commented Jun 27, 2024

That's great @longye-tian , please go ahead!

@mmcky
Copy link
Contributor

mmcky commented Jun 27, 2024

thanks @longye-tian and @jstac this has now been merged so closing.

@mmcky mmcky closed this as completed Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants