Folium
Folium¶
Folium is a great library in python which allows us to plot things on actual maps. If you do not yet have it installed, you will need to pip install it. As for how to use it, let’s begin with how to create your first map. We create a map object by calling folium.Map() and giving the arguments of location (the latitude and longitude as a list) and how far in we want the zoom to be in the beginning. Below are arguments that center the map on Boston.
import folium as folium
#Create a map of Boston
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
boston_map
Heatmap¶
One of the preliminary questions we should have when investigating this dataset is where most of these properties are. Different areas require different considerations. Also, as you will see, there are certain parts that people may not realize are not actually part of Boston such as cambridge, which makes for interesting land boundaries.
To visualize this, we can use a heatmap. This map will visualize areas with greater density of properties with brighter colors. The way we create a map like this begins with first getting a list of all the latitude and longitude coordinates we want to include. This is simple enough.
#Get the latitude and longitude coordinates
lat_lon = r1[['Latitude', 'Longitude']].values
The second step is to add the heatmap by first creating the object after importing it from folium.plugins with the coordinates then calling add_to() on it and passing the map we are adding it to.
from folium.plugins import HeatMap
#Create the map
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
#Add the heatmap
HeatMap(lat_lon).add_to(boston_map)
boston_map
Heatmap Radius¶
To make the density have more or less impact in terms of the area around it, you can toggle the radius argument in the heatmap. Look below at two examples.
#An example with radius 8
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
HeatMap(lat_lon, radius=8).add_to(boston_map)
boston_map
#An example with radius 8
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
HeatMap(lat_lon, radius=10).add_to(boston_map)
boston_map
Depending on your needs, a different radius may make the map more readable!
Drawing Rectangles¶
We are going to introduce the way to draw rectangles on to the map now. To do this we will first take a step back and talk about what our goals will be for it...
The purpose of drawing rectangles in this lesson is to build up to the point where we are able to actually find the average value of land or a property within a rectangle, then plot the rectangles with colors corresponding to the value in that area.
Step 1 is to make our first rectangle. We will begin by finding the maximum and minimum latitude and longitude to draw a box around the area which contains all of our properties.
#Find the min/max values
lat_min = r1['Latitude'].min()
lat_max = r1['Latitude'].max()
lon_min = r1['Longitude'].min()
lon_max = r1['Longitude'].max()
To draw the rectangle, we need four sets of coordinates, one for each corner. The following coordinates will be used to plot the bounds of our properties.
#Create the coordinates
coordinates = [[lat_min, lon_min],
[lat_min, lon_max],
[lat_max, lon_max],
[lat_max, lon_min]]
print(coordinates)
The rectangle is created with folium.vector_layers.Rectangle, and we pass it the coordinates. Then we use add_to() just like with the heatmap to add it to our map.
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
#Add the rectangle to the map
folium.vector_layers.Rectangle(coordinates).add_to(boston_map)
boston_map
Coloring the Rectangle¶
There are a few arguments that we wish to modify to color our rectangle. Right now we will just make it red, but in the next few minutes we will work to use the colors to represent values. Now the arguments we will be using are:
- color: The color of the border of the rectangle
- fill: A boolean value for whether or not to fill in the rectangle
- fill_color: The color for filling the rectangle
- fill_opacity: How opaque to make the rectangle, lower values make it more transparent
I am modifying the code above to give the rectangle a black outline, filled with red, that is not very transparent.
boston_map = folium.Map(location=[42.35, -71], zoom_start=11)
#Add the rectangle to the map
folium.vector_layers.Rectangle(coordinates, color='#000000', fill=True,
fill_color='#FF0000',fill_opacity=.8).add_to(boston_map)
boston_map