77

Let’s plot Airbnb Prices on a Map of Singapore

 4 years ago
source link: https://towardsdatascience.com/lets-plot-airbnb-prices-on-a-map-of-singapore-ddbcae44fb69?gi=d8823c590d1e
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Let’s plot Airbnb Locations and Prices on a Map of Singapore

Nov 6 ·3min read

IZRFfmf.png!web

Plotting data on map is easier than you’d think.

Let’s walk through plotting Airbnb home locations (colour-coded by price) on a map of Singapore. Why? Well, I like Singapore but I spend a small fortune on Airbnbs whenever I visit.

Download the dataset from Kaggle, then save it to the same directory as your jupyter notebook . You’ll need to login to Kaggle to download it.

Dataset: https://www.kaggle.com/jojoker/singapore-airbnb

The file is a CSV which we can easily preview with pandas.

import pandas as pd# you may have named it something different
df = pd.read_csv('data-sg-listings.csv')# take a look
df.head()

YFN32iq.png!web

Now let’s drop all homes with a price over 500 as outliers will throw off our color coding.

df = df[df['price'] < 500]

Perfect.

Here comes the hard part. Getting the map image of Singapore that we’ll eventually plot on top of. Here’s what I did.

I went to http://www.copypastemap.com and inputted the latitude and longitude of corner points that would form a bounding box around Singapore (1.15N,103.5E & 1.50N,104E). Then I took a screenshot and used the same points for the min/max of my x and y axes.

But save yourself the hassle and just screenshot my screenshot below. Then save it in the same directory.

fU3aAnR.png!web

Phew. Now the easy part. Plot our points on the map. Aka. plot our points on a scatter plot with the map image as a background.

import matplotlib.pyplot as plt# import our image 
singapore_img = mpimg.imread('singapore-map-3.png')# plot the data
ax = df.plot(
    kind="scatter", 
    x="longitude", 
    y="latitude", 
    figsize=(20,14),
    c="price", 
    cmap=plt.get_cmap("jet"),
    colorbar=True, 
    alpha=0.4,
)# use our map with it's bounding coordinates
plt.imshow(singapore_img, extent=[103.5,104,1.15, 1.50], alpha=0.5)            # add axis labels
plt.ylabel("Latitude", fontsize=20)
plt.xlabel("Longitude", fontsize=20)# set the min/max axis values - these must be the same as above
plt.ylim(1.15, 1.50)
plt.xlim(103.5, 104)plt.legend(fontsize=20)
plt.show()

And look at that.

IZRFfmf.png!web

The hardest for me was actually getting an image that included all the latitude/longitudes points I wanted to plot. Plotting the points is pretty easy thanks to pandas and matplotlib.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK