With so much Airbnb data available, it can be hard to get a grasp on the trends and demands in your area. Here, we’ll explore ways to visualize Airbnb listings using Python and popular visualization libraries.
Check out the Airbnb Listings endpoint, where you can retrieve structured data for listings around the world. You can enter a city or just use the default settings to get listings from all over the world. If you execute the endpoint, you’ll see an option to download the Expanded CSV results so we can use it in a visualization library:
Save the CSV somewhere handy, such as your desktop: ~/Desktop/airbnb_global.csv
.
Now, we’ll write a little Python code to begin our visualizations.
Loading into Pandas
Let’s first load our CSV into a Pandas dataframe:
import os
import pandas as pd
df = pd.read_csv(os.path.expanduser('~/Desktop/airbnb_global.csv'))
Pandas will automatically infer the column types for you. You can check out the column names with:
df.columns.values
Mapping with Cartopy
We’ll use Cartopy to visualize our listings on a map. Let’s start by importing the core modules we’ll need
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
ax = plt.axes([0, 0, 1, 1], projection=ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN, zorder=0)
ax.coastlines()
This will generate a blank world map as follows:
This is cool, but where’s our Airbnb data? Let’s now reference back to our Pandas dataframe and begin plotting our points.
Let’s first get the latitude and longitutes for our Airbnb listings:
longitudes = list(df['explore_tabs.sections.listings.listing.lng'])
latitudes = list(df['explore_tabs.sections.listings.listing.lat'])
Next, let’s make each point on the map proportional in size to the number of reviews about the listing, e.g.:
radiuses = (df['explore_tabs.sections.listings.listing.reviews_count'] / 50.0) ** 2
Lastly, let’s color each datapoint by price - higher priced listings will be more red and lower priced will be less intense.
import matplotlib.cm as cm
colors = [cm.hot(x) for x in list(df['explore_tabs.sections.listings.pricing_quote.can_instant_book'])]
We can now combine it all together and generate a world map of our Airbnbs:
ax.scatter(
longitudes,
latitudes,
c=colors,
s=radiuses,
zorder=2,
alpha=0.5)
Now we’ll get a nice map of Airbnb listings like this: