QuantEcon / lecture-python-intro

An Undergraduate Lecture Series for the Foundations of Computational Economics
https://intro.quantecon.org/
34 stars 17 forks source link

[lp_intro] change the visualization code #475

Closed longye-tian closed 3 days ago

longye-tian commented 5 days ago

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 commented 4 days ago

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 commented 4 days ago

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

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.75x1, label="iso-revenue lines",color='k',linewidth=0.75) ax.plot(x1, 5.375-0.75x1, 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:
<img width="580" alt="revised plot" src="https://github.com/QuantEcon/lecture-python-intro/assets/133612246/b963ed24-4c07-4262-8777-e74a486f6971">

This indeed looks much better! 

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

Best ❤️ 
Longye
jstac commented 4 days ago

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

mmcky commented 3 days ago

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