# Plot Lines In Folium

By | June 3, 2016

In this post I show you how to plot lines in Folium – the python module for plotting leaflet maps.

Like ‘markers’, lines are added to folium map objects with the add_to() method

```folium.PolyLine(points).add_to(my_map)
```

where “points” is a list of tuples containing latitude and longitude information, and “my_map” is a folium map object.

By default this will give an output similar to:

We can alter the line properties (colour, weight and opacity) with:

```folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)
```

# Plot Lines and Markers

It can also be useful to plot both lines and markers on the same map. This can be easily done by using the Marker.add_to() method, and looping over each of the coordinate pairs in our list:

```#add markers
for each in points:

```

# An Example

```import gpxpy
import gpxpy.gpx
import folium

gpx_file = open('path_to_gpx_file.gpx', 'r')

gpx = gpxpy.parse(gpx_file)
points = []
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
points.append(tuple([point.latitude, point.longitude]))
print(points)
ave_lat = sum(p[0] for p in points)/len(points)
ave_lon = sum(p[1] for p in points)/len(points)

# Load map centred on average coordinates
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)

for each in points:

# Save map
my_map.save("./gpx_berlin_withmarker.html")
```

## 6 thoughts on “Plot Lines In Folium”

1. lukebarker

hey cheers for all these excellent posts on Folium, it seems a great little library ðŸ™‚

2. J Szlomp

Please Obiwan you’re my only hope! I’ve been having a very difficult time getting my polylines to show up on my map. I have the below code snippet and if I remark out the Polyline lines the circle markers come up just fine. If I unremark them only the first set of circle markers in the loop show up and the polylines do not show either. I’ve looked at this for so long my eyes are crossing. I’ve tried adding “.add_to(map)” to the end of the each line but that hasn’t helped.

a = geolocator.geocode(alo)
b = geolocator.geocode(zlo)
a1= [aCoord.latitude, aCoord.longitude]
b1= [zCoord.latitude, zCoord.longitude]

fill=’true’,
popup= ‘Hi’,
fill_color=’blue’,
color = ‘clear’,
fill_opacity=1))

fill=’true’,
popup= ‘bye’,
fill_color=’red’,
color = ‘clear’,
fill_opacity=1))

1. deparkes

Hey,
Is this at all what you are trying to do?
import folium

points_a = [[1,50], [1.2,50.3], [1.23, 50.7]]
points_z = [[1,51], [1.2,51.3], [1.23, 51.7]]

# Load map centred on average coordinates
ave_lat = sum(p[0] for p in points_a)/len(points_a)
ave_lon = sum(p[1] for p in points_a)/len(points_a)
my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=9)

for each in points_a:
fill=’true’,
popup= ‘Hi’,
fill_color=’blue’,
color = ‘clear’,
fill_opacity=1))

for each in points_z:
fill=’true’,
popup= ‘Bye’,
fill_color=’yellow’,
color = ‘clear’,
fill_opacity=1))

# Save map
my_map.save(“./lines_withmarker.html”)

1. J Szlomp

Oh man I’m sorry I didn’t get any notification that you responded, thank you so much! ðŸ˜€
Would you happen to know if there is a built in offset for the markers? I’m mapping out hundreds of routes in a small region and they are all starting to overlap which is making it difficult to click on the markers to get details. I’m thinking a random offset might help but i’m unsure as to how to do that.

1. deparkes

No worries :-). I’m not sure of anything off the top of my head, although you might want to look into using marker clusters which group together markers depending on zoom level.

I had a simple go a this here – https://deparkes.co.uk/2016/07/08/uk-university-locations/ . I’m not sure how it would work with the lines between the markers though

1. J Szlomp

Oh man this is awesome! Even if the poly lines don’t end up looking right this is too cool! thanks for opening my eyes to folium awesomesauce!