0

I want to plot a dataset that basically shows the equilibrium point from a demand and supply curve. The graph for the demand and supply curve plots well but the equilibrium point graph doesn't seem to plot as the axes are different.

Code for the demand and supply curve.

def S(q):
    return (q**2)

def D(q):
    return (q - 20)**2

q = np.linspace(0, 16, 1000)

plt.plot(q, S(q), label = "Supply Curve")
plt.plot(q, D(q), label = "Demand Curve")
plt.title("Supply and Demand")
plt.legend(frameon = False)
plt.xlabel("Quantity $q$")
plt.ylabel("Price")

code for the Equilibrium price

q = sy.Symbol('q')
eq = sy.Eq(S(q), D(q))
sy.solve(eq)

plt.figure(figsize= (10, 8))#create figure and reset q to be numbers
q2 = np.linspace(0, 16, 1000)


plt.plot(q2, S(q2), label = "S Curve")#plot supply, demand, and equilibrium points
plt.plot(q2, D(q2), label = "D Curve")
plt.plot(10, 100, 'o', markersize = 14)


plt.title("Equilibrium point")#add titles and legend
plt.legend(frameon = False)
plt.xlabel("Quantity")
plt.ylabel("Price")


ax = plt.axes()#add arrow with annotation
ax.annotate('Equilibrium at (10, 100)', xy=(10,100), xytext=(10, 250), arrowprops=dict(facecolor='black'))

Expected Output

EXPECTED OUTPUT

Given Output

GIVEN OUTPUT

  • When I run the code that you have shown here, I get a plot showing your desired output. Can you explain in more detail what your problem is? – eandklahn Dec 03 '21 at 09:53
  • With `ax = plt.axes()` you create a new empty subplot on top of the existing plot. You probably want to use `ax = plt.gca()` to get the current subplot (gca: ["get current axes"](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.gca.html)). See also [matplotlib's object-oriented interface](https://matplotlib.org/matplotblog/posts/pyplot-vs-object-oriented-interface/). – JohanC Dec 03 '21 at 11:34

0 Answers0