Showing Buenos Aires properties in a Map with Plotly
By: Sebastian Carmona A.
Tags: Plotly, Maps, Data Science, Data, Mapping

The code has various parts:

fig = px.scatter_mapbox(data, lat="lat", lon="lon", color="l2",hover_name="property_type", size="price", hover_data=["price", "l3"],color_continuous_scale=px.colors.cyclical.IceFire, zoom=10, height=1000, center={"lat":-34.60652, "lon":-58.43557})

First I had to specify my dataframe where the info is, in this case is called “data”. Then, specify the name of the columns where the Latitude and Longitude is in this case “lat” and “lon”. The “color” part of the function is how the properties will be divided, like the hue. The “hover_name” will be the title I wanted to see when we step the mouse over a property. The “size” is how I wanted to visualize the properties, in this case if the bubble (property) is very big, it is because the price is big. The “hover_data” is the information I wanted to see when I step over a property. And finally, the “center” is where I wanted to center my ap as soon as it loads.

I did different types of maps with plotly, but what will happen if I didn’t have the “lat” and “lon” of each property? How could I graph them on a map?

Graphing on a map without having Latitude and Longitude at first in our data

Since I already had the latitude and longitude of each property I decided to graph each Borough on a map, for those I didn’t had their Latitude and Longitude.

Using Geopy library

The Geopy library help us to search latitude and longitude for what we are looking for, in this case the Borough. We can see it here:

First I needed to put in the most explicit way the Borough that I needed to be searched with Geopy.

precio_barrio['l3'] = 'Argentina, Buenos Aires, ' + precio_barrio['l3'].astype(str)

Image by Author

I was searching for “l3” that is the Borough, and the price is the mean price of the properties on each Borough.

After putting in the Borough in the most explicit way, we make the search:

from geopy.geocoders import Nominatim geolocator = Nominatim() precio_barrio["location"] = precio_barrio["l3"].apply(geolocator.geocode) precio_barrio["latitude"] = precio_barrio["l3"].apply(geolocator.geocode).apply(lambda x: (x.latitude)) precio_barrio["longitude"] = precio_barrio["l3"].apply(geolocator.geocode).apply(lambda x: (x.longitude))

This was the result:

Image by Author

Now that I had the “Latitude” and “Longitude” by Borough, I can map it the way we did it earlier with different types of maps layers.

Map # 1

fig = px.scatter_geo(precio_barrio, lat="latitude", lon="longitude", color="l2", hover_name="l3", size="price", projection="natural earth", center={"lat":-34.60652, "lon":-58.43557}) fig.show()

Image by Author

2. Map #2

px.set_mapbox_access_token("Yor-Access-Token") fig = px.scatter_mapbox(precio_barrio, lat="latitude", lon="longitude", color="price",hover_name="l3", size="price", color_continuous_scale=px.colors.cyclical.IceFire, zoom=10, height=1000, center={"lat":-34.60652, "lon":-58.43557}) fig.show()

Image by Author

3. Map #3

px.set_mapbox_access_token("Yor-Access-Token") fig = px.scatter_mapbox(precio_barrio, lat="latitude", lon="longitude", color="l2",hover_name="l3", size="price", color_continuous_scale=px.colors.cyclical.IceFire, zoom=10, height=1000, center={"lat":-34.60652, "lon":-58.43557}) fig.show()

Image by Author

Finally, even though we search each Borough with the Geopy library, the Latitude and Longitude weren't so exactly like we wish them to be. This is because we need to put the address or something more specific for Geopy Nominatum found the exact latitude and longitude by Borough.

You can find the original notebook for this project in here.

Here in my Github.

Anything to improve and add, I am all ears!!