Created
May 16, 2019 13:21
-
-
Save L-Lewis/dd6265687b0112fd36d03f22243a34c6 to your computer and use it in GitHub Desktop.
Using geopandas to plot the number and median price of Airbnb listings in each London borough
This file contains 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 geopandas as gpd | |
# Importing the London borough boundary GeoJSON file as a dataframe in geopandas | |
map_df = gpd.read_file('data/neighbourhoods.geojson') | |
# Creating a dataframe of listing counts and median price by borough | |
borough_df = pd.DataFrame(df.groupby('borough').size()) | |
borough_df.rename(columns={0: 'number_of_listings'}, inplace=True) | |
borough_df['median_price'] = df.groupby('borough').price.median().values | |
# Joining the dataframes | |
borough_map_df = map_df.set_index('neighbourhood').join(borough_df) | |
# Plotting the number of listings in each borough | |
fig1, ax1 = plt.subplots(1, figsize=(15, 6)) | |
borough_map_df.plot(column='number_of_listings', cmap='Blues', ax=ax1) | |
ax1.axis('off') | |
ax1.set_title('Number of Airbnb listings in each London borough', fontsize=14) | |
sm = plt.cm.ScalarMappable(cmap='Blues', norm=plt.Normalize(vmin=0, vmax=9000)) | |
sm._A = [] # Creates an empty array for the data range | |
cbar = fig1.colorbar(sm) | |
plt.show() | |
# Plotting the median price of listings in each borough | |
fig2, ax2 = plt.subplots(1, figsize=(15, 6)) | |
borough_map_df.plot(column='median_price', cmap='Blues', ax=ax2) | |
ax2.axis('off') | |
ax2.set_title('Median price of Airbnb listings in each London borough', fontsize=14) | |
sm = plt.cm.ScalarMappable(cmap='Blues', norm=plt.Normalize(vmin=min(borough_map_df.median_price), vmax=max(borough_map_df.median_price))) | |
sm._A = [] # Creates an empty array for the data range | |
cbar = fig2.colorbar(sm) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment