frank
frank

Reputation: 3608

is it possible to colour a rectangle in folium python

I have a map:

map_osm = folium.Map(location=[51.366975, 0.039039],zoom_start=12)
map_osm

I want to be able to add a rectangle marker that I can colour according to some statistic. I found a polygon_marker (http://bl.ocks.org/wrobstory/5609786) but receive the error 'Map' object has no attribute 'polygon_marker' when I try:

map_osm.polygon_marker(location=[45.5132, -122.6708], popup='Hawthorne Bridge',
                     fill_color='#45647d', num_sides=4, radius=10)

In the final product, I would like to have lots of rectangles colour coded.

Any suggestions

Upvotes: 3

Views: 6022

Answers (2)

mitbal
mitbal

Reputation: 355

After little digging into the the docs and source code and quick experimentation, you can just input parameter fill_color='blue' when you initialize Rectangle object.

Example code:

m = folium.Map(
    location=[-6.237933179178703, 106.81783770824106],
    zoom_start=13,
    tiles='Stamen Toner'
)

folium.Rectangle(bounds=points, color='#ff7800', fill=True, fill_color='#ffff00', fill_opacity=0.2).add_to(m)

Result enter image description here

Upvotes: 6

frank
frank

Reputation: 3608

A little more research, I found:

grid_pt=(51.4,0.05)
W=grid_pt[1]-0.005
E=grid_pt[1]+0.005
N=grid_pt[0]+0.005
S=grid_pt[0]-0.005


upper_left=(N,W)
upper_right=(N,E)
lower_right=(S,E)
lower_left=(S,W)
line_color='red'
fill_color='red'
weight=2
text='text'
edges = [upper_left, upper_right, lower_right, lower_left]
map_osm = folium.Map(location=[latty, longy],zoom_start=14)
map_osm.add_child(folium.vector_layers.Polygon(locations=edges, color=line_color, fill_color=fill_color,
                                              weight=weight, popup=(folium.Popup(text))))

this works to add a single rectangle, then loop to get more

Upvotes: 3

Related Questions