---
title: "Economics Books Figure"
format:
html:
code-fold: true
author: Lifeng Ren
date: Jul/07/2023
---
## For the following table, there is a Supply change
| Price per Pound | Quantity Supplied 2014 | Quantity Supplied 2015 | Quantity Demanded |
|---|---|---|---|
| 2.00 | 80 | 400 | 840 |
| 2.25 | 120 | 480 | 680 |
| 2.50 | 160 | 550 | 550 |
| 2.75 | 200 | 600 | 450 |
| 3.00 | 230 | 640 | 350 |
| 3.25 | 250 | 670 | 250 |
| 3.50 | 270 | 700 | 200 |
Using code to visulize it
```{python}
import pandas as pd
import matplotlib.pyplot as plt
# Create a DataFrame with your data
df = pd.DataFrame({
"Price_per_Pound": [2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50],
"Quantity_Supplied_2014": [80, 120, 160, 200, 230, 250, 270],
"Quantity_Supplied_2015": [400, 480, 550, 600, 640, 670, 700],
"Quantity_Demanded": [840, 680, 550, 450, 350, 250, 200]
})
# Save DataFrame to a CSV file
df.to_csv("data.csv", index=False)
# Read data from CSV file
data = pd.read_csv("data.csv")
# Plot the demand curve
plt.plot(data["Quantity_Demanded"], data["Price_per_Pound"], label='Demand', color='blue')
# Plot the supply curve for 2014
plt.plot(data["Quantity_Supplied_2014"], data["Price_per_Pound"], label='Supply 2014', color='green')
# Plot the supply curve for 2015
plt.plot(data["Quantity_Supplied_2015"], data["Price_per_Pound"], label='Supply 2015', linestyle='--', color='green')
# Add an arrow annotation to indicate the shift direction
mid_index = len(data) // 2
plt.annotate('', xy=(data["Quantity_Supplied_2015"].iloc[mid_index], data["Price_per_Pound"].iloc[mid_index]),
xytext=(data["Quantity_Supplied_2014"].iloc[mid_index], data["Price_per_Pound"].iloc[mid_index]),
arrowprops=dict(facecolor='red', shrink=0.05))
# Add labels and legend
plt.xlabel('Quantity')
plt.ylabel('Price per Pound')
plt.legend()
# Display the plot
plt.show()
```
## How to understand this in a mathmatical way?
### Let us consider the most simple case with linear demand and supply curves
Demand Curve:
\begin{equation}
D(q) = 10-x
\end{equation}
Supply Curve in 2014:
\begin{equation}
S(q) = x
\end{equation}
Supply Curve in 2015:
\begin{equation}
S(q) = x - 2
\end{equation}
```{python}
import matplotlib.pyplot as plt
import numpy as np
# Define a function for demand curve
def demand_curve(x):
return 10 - x
# Define a function for supply curve
def supply_curve(x):
return x
# Define a function for shifted supply curve
def shifted_supply_curve(x):
return x - 2 # Decrease in quantity supplied at every price level
# Create a range of quantity values
quantity = np.linspace(0, 10, 100)
# Calculate price values for demand and supply curves
demand_price = demand_curve(quantity)
supply_price = supply_curve(quantity)
shifted_supply_price = shifted_supply_curve(quantity)
# Plot the demand curve
plt.plot(quantity, demand_price, label='Demand', color='blue')
# Plot the supply curve
plt.plot(quantity, supply_price, label='Supply', color='green')
# Plot the shifted supply curve
plt.plot(quantity, shifted_supply_price, label='Supply (shifted)', linestyle='--', color='green')
# Add an arrow annotation to indicate the shift direction
mid_index = len(quantity) // 2
plt.annotate('', xy=(quantity[mid_index], shifted_supply_price[mid_index]),
xytext=(quantity[mid_index], supply_price[mid_index]),
arrowprops=dict(facecolor='red', shrink=0.05))
# Add labels and legend
plt.xlabel('Quantity')
plt.ylabel('Price')
plt.legend()
# Display the plot
plt.show()
```
## Given the real data, how to look at them combined with the math?
We can use the following code to fit the actual data points first, and then see the supply shifting.
```{python}
import numpy as np
import matplotlib.pyplot as plt
# Define your data
Price_per_Pound = np.array([2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50])
Quantity_Supplied_2014 = np.array([80, 120, 160, 200, 230, 250, 270])
Quantity_Supplied_2015 = np.array([400, 480, 550, 600, 640, 670, 700])
Quantity_Demanded = np.array([840, 680, 550, 450, 350, 250, 200]) # Assuming this is your demand data
# Fit lines to the data
slope_2014, intercept_2014 = np.polyfit(Price_per_Pound, Quantity_Supplied_2014, 1)
slope_2015, intercept_2015 = np.polyfit(Price_per_Pound, Quantity_Supplied_2015, 1)
slope_demand, intercept_demand = np.polyfit(Price_per_Pound, Quantity_Demanded, 1) # Fitting demand data
# Generate coordinates for the best fit lines
best_fit_line_2014 = slope_2014 * Price_per_Pound + intercept_2014
best_fit_line_2015 = slope_2015 * Price_per_Pound + intercept_2015
best_fit_line_demand = slope_demand * Price_per_Pound + intercept_demand # Line for demand
# Plot the data points
plt.scatter(Price_per_Pound, Quantity_Supplied_2014, color='blue', label='Quantity Supplied 2014')
plt.scatter(Price_per_Pound, Quantity_Supplied_2015, color='red', label='Quantity Supplied 2015')
plt.scatter(Price_per_Pound, Quantity_Demanded, color='green', label='Quantity Demanded') # Plotting demand data
# Plot the best fit lines
plt.plot(Price_per_Pound, best_fit_line_2014, color='blue', linestyle='-')
plt.plot(Price_per_Pound, best_fit_line_2015, color='red', linestyle='--')
plt.plot(Price_per_Pound, best_fit_line_demand, color='green', linestyle='-') # Plotting demand line
# Add an arrow showing the shift in supply from 2014 to 2015
mid_price = Price_per_Pound[len(Price_per_Pound)//2]
plt.annotate("", xy=(mid_price, slope_2015*mid_price+intercept_2015), xycoords='data',
xytext=(mid_price, slope_2014*mid_price+intercept_2014), textcoords='data',
arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color='red'))
# Add labels and a legend
plt.xlabel('Price per Pound')
plt.ylabel('Quantity')
plt.legend()
# Show the plot
plt.show()
```
Demand Curve:
\begin{equation}
D(q) = 1645-x
\end{equation}
Supply Curve in 2014:
\begin{equation}
S(q) = -166.43 +128.57 \times x
\end{equation}
Supply Curve in 2015:
\begin{equation}
S(q) = 38.93 + 195.71 \times x
\end{equation}
##### Textbook license and version note
This textbook is an Open-Educational Resource (OER) textbook, licensed under an open-source Creative Commons Share-Alike (CC-SA) license, and is adapted from another OER textbook provided by [OpenStax.org](https://openstax.org/) at <https://openstax.org/details/books/principles-microeconomics-3e>. See the [Github repository](https://github.com/jandrewjohnson/open_principles_of_microeconomics_dev) for details and to (hopefully) edit/correct/improve the book yourself!