image_output_2.png image_output_1.png
import matplotlib.pyplot as plt

import numpy as np

def priceOverTime(data, label):
    """Plot price over time"""
priceOverTime(newdf3, 'California')
priceOverTime(newdf4, 'Colorado')
priceOverTime(newdf5, 'Michigan')

def priceOverTime2(data, label):
    pass
priceOverTime2(newdf6, 'San Francisco')
priceOverTime2(newdf7, 'Denver')
priceOverTime2(newdf8, 'Detroit')

State_raw_house = State_house.groupby(['RegionName', State_house.Date.dt.year])['ZHVI_SingleFamilyResidence'].mean().unstack()
State_raw_house.columns.name = None
State_raw_house = State_raw_house.reset_index()
State_raw_house = State_raw_house[['RegionName', 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017]]
State_raw_house = State_raw_house.dropna()
Feature = State_raw_house['RegionName']
weightage = State_raw_house[2010]
total = State_raw_house[2017]
percent = (State_raw_house[2017] - State_raw_house[2010]) / State_raw_house[2010] * 100
mid_pos = (State_raw_house[2010] + State_raw_house[2017]) / 2
weightage = np.array(weightage)
Feature = np.array(Feature)
total = np.array(total)
percent = np.array(percent)
mid_pos = np.array(mid_pos)
idx = percent.argsort()
Feature, total, percent, mid_pos, weightage = [np.take(x, idx) for x in [Feature, total, percent, mid_pos, weightage]]
s = 1
size = []
for i, cn in enumerate(weightage):
    s = s + 1
    size.append(s)
fig, ax = plt.subplots(figsize=(13, 13))
ax.scatter(total, size, marker='o', color='lightBlue', s=size, linewidths=10)
ax.scatter(weightage, size, marker='o', color='LightGreen', s=size, linewidths=10)
ax.set_xlabel('Home Value')
ax.set_ylabel('States')
ax.spines['right'].set_visible(False)
ax.grid()
for i, txt in enumerate(Feature):
    ax.annotate(txt, (720000, size[i]), fontsize=12, rotation=0, color='Red')
    ax.annotate('.', xy=(total[i], size[i]), xytext=(weightage[i], size[i]), arrowprops=dict(facecolor='LightGreen', shrink=0.06))
for i, pct in enumerate(percent):
    ax.annotate(str(pct)[0:4], (mid_pos[i], size[i]), fontsize=12, rotation=0, color='Brown')
ax.annotate('2010 Home Value', (300000, 26), fontsize=14, rotation=0, color='Green')
ax.annotate('2017 Home Value', (300000, 25), fontsize=14, rotation=0, color='Blue')
ax.annotate('w/ Percent Change', (300000, 24), fontsize=14, rotation=0, color='Brown')