Sunday, April 16, 2017

Python Matplotlib BarPlot Example

Matplotlib provide a MATLAB like plotting framework.
Here is an example of bar charts using Matplotlib.

Zillow rent index data used for plotting.
Zillow Rent Index (ZRI) track the monthly median rent in particular geographical regions.

Source Data



Zillow Rent Index data at City, State and Country level.




Source Code

 import pandas as pd  
 import io  
 import requests  
 ########## Read City level CSV and Filter specific City ( Belmont for example)  
 ##  
 citydata=pd.read_csv('City_Zri_AllHomesPlusMultifamily_Summary.csv')  
 citydata=citydata[['State','RegionName','Zri','YoY']].where((citydata['RegionName'] == 'Belmont') & (citydata['State'] == 'CA') ).dropna()  
 citydata=citydata.reset_index()  
 citydata=citydata[['RegionName','Zri','YoY']]  
 ##  
 ########## Read County level CSV and Filter specific City ( SanMateo for example)  
 ##  
 countydata=pd.read_csv('County_Zri_AllHomesPlusMultifamily_Summary.csv')  
 countydata=countydata[['State','RegionName','Zri','YoY']].where((countydata['RegionName'] == 'San Mateo')).dropna()  
 countydata=countydata.reset_index()  
 countydata=countydata[['RegionName','Zri','YoY']]  
 ##  
 ########## Read State level CSV and Filter specific City ( California for example)  
 ##  
 statedata=pd.read_csv('State_Zri_AllHomesPlusMultifamily_Summary.csv')  
 statedata=statedata[['RegionName','Zri','YoY']].where((statedata['RegionName'] == 'California')).dropna()  
 statedata=statedata.reset_index()  
 statedata=statedata[['RegionName','Zri','YoY']]  
 ##########  
 usadata=pd.read_csv('State_Zri_AllHomesPlusMultifamily_Summary.csv')  
 usadata=usadata[['RegionName','Zri','YoY']].where((usadata['RegionName'] == 'United States')).dropna()  
 usadata=usadata.reset_index()  
 usadata=usadata[['RegionName','Zri','YoY']]  
 ##########  
 ##  
 ########## Combine City, County, State and National level data  
 ##  
 zriData=pd.concat([usadata,statedata,countydata,citydata])  
 zri=zriData['Zri']  
 ##  
 ########## Plot  
 ##  
 %matplotlib notebook  
 import matplotlib.pyplot as plt  
 import numpy as np  
 Regions = ('United States', 'California', 'San Mateo County', 'Belmont City')  
 colors = ['grey','grey','grey','red']  
 y_pos = np.arange(len(Regions))  
 fig, ax = plt.subplots()  
 ax.bar(y_pos, zri,edgecolor='grey',width=0.8,alpha=0.4,color=colors)  
 plt.xticks(y_pos, Regions)  
 rects = ax.patches  
 labels = [i for i in zri]  
 for rect, label in zip(rects, labels):  
   height = rect.get_height()  
   ax.text(rect.get_x() + rect.get_width()/2, height + 5, label, ha='center', va='bottom',weight='bold')  
 ax.axes.get_yaxis().set_visible(False)  
 ax.set_frame_on(False)  
 plt.title('Zillow Value Index Comparison on City, County, State and County Levels')  
 plt.xlabel('Regions',weight='bold')  



Output


No comments:

Post a Comment