Scatter Plots on Maps using Plotly | by Raj Sangani | Towards Data Science

Plotly is a powerful visualisation library that provides amazing capabilities like interactive, dynamic, easy to use, and highly detailed plots. Plotly Express is a built-in part of the Plotly library that provides high-level APIs which require very little code to plot a variety of figures. We will be using Plotly express in combination with Mapbox. Mapbox is a location data platform that provides various APIs for Maps, routing and navigation.
In this article, we will be using the San Francisco Airbnb listings dataset from Kaggle to analyse the relation between prices, types of accommodations and their areas visually. By simply looking at the map we shall see how the area affects the price.
This data has a lot of columns, but the ones concerning this article are latitude, longitude, price and room_type. The price is the price per night and the room_type indicates whether the Airbnb is a hotel, a private room, a shared room or an entire house.
Here is what the data looks like
Firstly we will need a Mapbox access token. Don’t worry it's completely free up to a certain amount of calls and it doesn’t require any card details when signing up. You can get your token here for no cost. After signing up the token will be under your account page.Convert the price to a float. The price column currently contains strings with the ‘$’ symbol and commas. For example $1,200. Here is a super quick way of converting the entire column to float values. This will con
Firstly we will need a Mapbox access token. Don’t worry it's completely free up to a certain amount of calls and it doesn’t require any card details when signing up. You can get your token here for no cost. After signing up the token will be under your account page.
Convert the price to a float. The price column currently contains strings with the ‘$’ symbol and commas. For example $1,200. Here is a super quick way of converting the entire column to float values. This will con
import localelocale.setlocale(locale.LC_ALL, 'en_US.UTF8')data['price']=data['price'].apply(lambda x: locale.atof(x.strip("$")))
3. Now comes the best part, the plot. If you are running this code in Colab which I recommend you don’ need to install Plotly. Else you can pip install it.
import plotly.express as px
After this, we just need to write the code shown below. Here we first pass in our Mapbox access token. Then we use the function px.scatter_mapbox with the following arguments.
data: This refers to our data frame
lat=” latitude”: This points to the latitude column in our data frame.
lon=” longitude”: This points to the longitude column in our data frame.
color=”room_type”: This basically colours each data point based on its value in the room_type column. Since we have four different room types we will have four colours.
size=” price”: This is the interesting part, each Airbnb will be denoted by a bubble that is proportional in size to its price. Cheaper Airbnbs will be denoted by smaller bubbles whereas expensive ones will be portrayed by larger bubbles.
The other arguments are just utilities to help make the plot easier to view.
px.set_mapbox_access_token("Your mapbox API Token")
fig = px.scatter_mapbox(data, lat="latitude", lon="longitude",color="room_type", size="price",
                  color_continuous_scale=px.colors.cyclical.IceFire, size_max=20,zoom=12)
fig.show()
4. Feel free to play around with the options in the top right corner of the plot for utilities like zooming, selecting specific areas, or downloading the graph as an image. You can zoom in to a particular region to see how detailed the map is Also notice how the hotel’s information is displayed when we hover over a bubble! Clicking on any room type in the legend will remove all rooms of that type from the map!
We see that some trends make sense. Before jumping to conclusions however note that the Airbnb price depends on various amenities and other features like the size, services, host popularity etc. However, we can still see some general trends.
Houses near Pacific Heights and Russian Hill have prices higher than those in other parts of San Francisco.On zooming out, we notice that the density of houses is not uniform. There are many more rooms on the right half of the map than on the left.We see that most rooms are either full-fledged apartments or private rooms.There is an unusually high priced apartment costing 9999 dollars near Carl St. Wonder what that is !
Houses near Pacific Heights and Russian Hill have prices higher than those in other parts of San Francisco.
On zooming out, we notice that the density of houses is not uniform. There are many more rooms on the right half of the map than on the left.
We see that most rooms are either full-fledged apartments or private rooms.
There is an unusually high priced apartment costing 9999 dollars near Carl St. Wonder what that is !
Thanks to Plotly Express and Mapbox, we can plot such powerful and information-rich graphs with almost no effort. This is just one kind of GeoPlot that Plotly offers. You can check out their other Map plots here.
If you liked this article here are some more!
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
Check out my GitHub for some other projects. You can contact me here. Thank you for your time!