TypeError received while trying to create Folium Colorline

Question:

I’m trying to create ColorLine for set of latitude and longitude coordinates. Code for the same is below,

map_object = folium.Map(location=[15.235683, 75.167737], zoom_control=True, zoom_start = 10, control_scale=True)
coordinates = [(15.564746,74.819551), (15.392602,75.173812),(15.392602,75.173812),(15.413797,74.735791)]
color=['red','green','blue']  # should be of length len(coordinates) -1
line = features.ColorLine(coordinates,colors = color , weight= 10)
line.add_to(map_object)

But on execution, am getting

Traceback (most recent call last):
  File "<xxx>Interactive mapsmain.py", line 12, in <module>       
    line = features.ColorLine(coordinates,colors = color , weight= 10)
  File "<xxx>Python39libsite-packagesfoliumfeatures.py", line 1892, in __init__
    if colormap is None:
  File "<xxx>Python39libsite-packagesbrancacolormap.py", line 235, in __init__
    self.index = [vmin + (vmax-vmin)*i*1./(n-1) for i in range(n)]
  File "<xxx>Python39libsite-packagesbrancacolormap.py", line 235, in <listcomp>
    self.index = [vmin + (vmax-vmin)*i*1./(n-1) for i in range(n)]
TypeError: unsupported operand type(s) for -: 'str' and 'str'

I understood that vmin and vmax are string and ‘-‘ is not supported for strings. But how to resolve this. Is this a bug at folium package?

Tried modifying the value of colors to string and list of strings.

Expected:

colors : list-like object with at least two colors.
...
...
* a color name or shortcut (e.g: `"y"` or `"yellow"`)

Since in colormap.py-> class LinearColormap has this detailed, I expected passing string values of color should get handled.

Asked By: Jose25X

||

Answers:

folium.features‘s ColorLine doesn’t accept str for colors, but we can input RGB values using a for loop:

for color in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]:
    features.ColorLine(coordinates, colors=color, weight=10).add_to(map_object)

enter image description here

There is more control over the colors with colormap than colors, and specifying both folium will ignore colors; however, if we draw more coordinates (with different weights to make it more obvious), it seems folium maxes out at 2 colors:

coordinates = [[15.564746,74.819551], [15.392602,75.253812],[15.392702,75.193812],[15.413797,74.785791],[15.452702,75.103812],[15.473797,74.985791]]
color = [[1, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]]
for index, _color in enumerate(['red', 'green', 'blue', 'yellow']):
    features.ColorLine(coordinates[index:], colors=color[index], colormap=[[0, 1, 0], [0, 0, 1], [1, 0, 0], [0, 0, 1]], weight=index + 4).add_to(map_object)

enter image description here

We can enumerate coordinates and give that to colors instead of a list of colors, but then it only takes the first color and ignores the rest:

for index, _color in enumerate(coordinates):
    features.ColorLine(coordinates[index:], colors=_color, colormap=[[1, 1, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0]], weight=index + 4).add_to(map_object)

enter image description here

Maybe I need to play around with features.ColorLine more but yes it isn’t very flexible. The number of colors that can be added seems to max out at two (i.e. first and last colors, ignores the rest), though you can draw as many lines as you want (using those two colors).

Answered By: Ori Yarden