Created
August 8, 2024 04:21
-
-
Save JettIsOnTheNet/4e7914865efee840de5b99a9e4d8743d to your computer and use it in GitHub Desktop.
Python scripts that generate charts for Income vs Housing Cost in the United States via FRED 1984-2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| data1 = pd.read_csv('MEHOINUSA646N.csv') | |
| data2 = pd.read_csv('ASPUS.csv') | |
| data1['DATE'] = pd.to_datetime(data1['DATE']) | |
| data2['DATE'] = pd.to_datetime(data2['DATE']) | |
| fig, ax1 = plt.subplots(figsize=(12, 6)) | |
| ax1.plot(data1['DATE'], data1['MEHOINUSA646N'], label='Median Household Income', color='blue') | |
| ax1.set_xlabel('Year') | |
| ax1.set_ylabel('Median Household Income (USD)', color='blue') | |
| ax1.tick_params(axis='y', labelcolor='blue') | |
| ax2 = ax1.twinx() | |
| ax2.plot(data2['DATE'], data2['ASPUS'], label='Average Sale Price of Houses', color='green') | |
| ax2.set_ylabel('Average Sale Price of Houses (USD)', color='green') | |
| ax2.tick_params(axis='y', labelcolor='green') | |
| plt.title('Median Household Income vs. Average Sale Price of Houses in the USA') | |
| fig.legend(loc="upper left", bbox_to_anchor=(0.1,0.9)) | |
| plt.grid(True) | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| data1 = pd.read_csv('MEHOINUSA646N.csv') | |
| data2 = pd.read_csv('ASPUS.csv') | |
| data1['DATE'] = pd.to_datetime(data1['DATE']) | |
| data2['DATE'] = pd.to_datetime(data2['DATE']) | |
| plt.figure(figsize=(12, 6)) | |
| plt.plot(data1['DATE'], data1['MEHOINUSA646N'], label='Median Household Income', color='blue') | |
| plt.plot(data2['DATE'], data2['ASPUS'], label='Average Sale Price of Houses', color='green') | |
| plt.xlabel('Year') | |
| plt.ylabel('Value (in USD)') | |
| plt.title('Median Household Income vs. Average Sale Price of Houses in the USA') | |
| plt.legend() | |
| plt.grid(True) | |
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| data1 = pd.read_csv('MEHOINUSA646N.csv') | |
| data2 = pd.read_csv('ASPUS.csv') | |
| data1['DATE'] = pd.to_datetime(data1['DATE']) | |
| data2['DATE'] = pd.to_datetime(data2['DATE']) | |
| data1 = data1[data1['DATE'] >= '1984-01-01'] | |
| data2 = data2[data2['DATE'] >= '1984-01-01'] | |
| data1['Pct_Increase_Income'] = (data1['MEHOINUSA646N'] / data1['MEHOINUSA646N'].iloc[0]) * 100 | |
| data2['Pct_Increase_Price'] = (data2['ASPUS'] / data2['ASPUS'].iloc[0]) * 100 | |
| max_percentage = max(data1['Pct_Increase_Income'].max(), data2['Pct_Increase_Price'].max()) | |
| fig, ax1 = plt.subplots(figsize=(12, 6)) | |
| ax1.plot(data1['DATE'], data1['Pct_Increase_Income'], label='Percentage Increase in Median Household Income', color='blue') | |
| ax1.set_xlabel('Year') | |
| ax1.set_ylabel('Percentage Increase in Median Household Income', color='blue') | |
| ax1.tick_params(axis='y', labelcolor='blue') | |
| ax1.set_ylim(0, max_percentage) # NORMALIZE SCALES | |
| ax2 = ax1.twinx() | |
| ax2.plot(data2['DATE'], data2['Pct_Increase_Price'], label='Percentage Increase in Average Sale Price of Houses', color='green') | |
| ax2.set_ylabel('Percentage Increase in Average Sale Price of Houses', color='green') | |
| ax2.tick_params(axis='y', labelcolor='green') | |
| ax2.set_ylim(0, max_percentage) # NORMALIZE SCALES | |
| plt.title('Percentage Increase in Median Household Income vs. Average Sale Price of Houses in the USA (Starting from 1984)') | |
| fig.legend(loc="upper left", bbox_to_anchor=(0.1,0.9)) | |
| plt.grid(True) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment