-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I add a legend to a folium map? #528
Comments
We only have a built-in legend for the |
Legend creation would be great! |
According to this page's latest example, you just need to: import branca.colormap as cm
colormap = cm.linear.Set1.scale(0, 35).to_step(10)
colormap.caption = 'A colormap caption'
m.add_child(colormap) But :
I'll try to PR this in branca, but it would be interting if someone create a folium plugin that enable to put a control in a map with any kind of html. |
BTW: we could have a method in |
BTW we are using a using a language for Legends can be complex, like those with a callback that when clicked navigate to a map position or highlight a region, or they can be simple static images. I believe that the former is quite hard to implement in a generic way that #537 could use |
One could take inspiration from Leaflet for R. |
A workaround is to inject CSS-style in the FeatureGroup-name The https://github.com/python-visualization/folium/blob/master/folium/map.py#L105 import folium
m = folium.Map((51, 6), tiles='stamentoner', zoom_start=7)
group0 = folium.FeatureGroup(name='<span style=\\"color: red;\\">red circles</span>')
for lat, lng in zip(range(500, 520), range(50,70)):
folium.CircleMarker((lat/10, lng/10), color='red', radius=2).add_to(group0)
group0.add_to(m)
group1 = folium.FeatureGroup(name='<span style=\\"color: blue;\\">blue circles</span>')
for lat, lng in zip(range(500,520), range(70,50,-1)):
folium.CircleMarker((lat/10, lng/10), color='blue', radius=2).add_to(group1)
group1.add_to(m)
folium.map.LayerControl('topright', collapsed=False).add_to(m)
m |
I had a similar need but for a categorical legend. Here's my solution that I'd like to share ( I really like that it's dragable as well.): I'd love to see something like this get added to folium, but I understand that there are lots of moving parts. If there's any way I can help on a PR I'd be happy to, but I haven't contributed to folium before. -- Colin |
@talbertc-usgs solution is very nice. Thanks for sharing. The only problem is that it takes a lot to load (is it downloading any very big css?) |
Hi, your template looks very handsome. Would you mind if I implement it into a small project I'm working on? |
@talbertc-usgs Thanks for that solution! Do you know if there's a way to bind the legend element to a map layer so it dis/appears when the layer is selected in |
Hi all, @alex-gottlieb, did you find any solution to hide the legend when the layer is unselected ? I'am also interested in alternatives to make dynamic legend depending on selected layers. Cheers, Guillaume |
Many thanks Colin, do you have any ideas on how make the html script "formattable" ? some function to pass to the html script number of colors, color label etc.. ? since with triple quotation marks I find difficult to make the script formattable |
Try putting a f in front of the triple quote and f string formatting |
tried but it gives error "'% macro html(this, kwargs) %'"due to the % i think. |
You're right. You'd need to escape the existing curly braces first: https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo |
Thank you for this, you wouldn't mind if I used it for a small academic purpose? |
Please use it however you would like.
…-- Colin
From: Khushali Thakkar <[email protected]>
Sent: Sunday, April 26, 2020 10:45 AM
To: python-visualization/folium <[email protected]>
Cc: Talbert, Colin <[email protected]>; Mention <[email protected]>
Subject: [EXTERNAL] Re: [python-visualization/folium] How can I add a legend to a folium map? (#528)
I had a similar need but for a categorical legend.
Here's my solution that I'd like to share ( I really like that it's dragable as well.):
http://nbviewer.jupyter.org/gist/talbertc-usgs/18f8901fc98f109f2b71156cf3ac81cd
I'd love to see something like this get added to folium, but I understand that there are lots of moving parts. If there's any way I can help on a PR I'd be happy to, but I haven't contributed to folium before.
-- Colin
Thank you for this, you wouldn't mind if I used it for a small academic purpose?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#528 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AFQ6A2XZATSXEFRXSSE3XBDRORQHRANCNFSM4CTCYX7Q>.
|
Hey folks, based on @talbertc-usgs 's snippet, i created a slightly more flexible categorical legend, which you can view and use (under the MIT license) from this Github repo. |
But how to put the legend on top of the map, that is, as a child rather than a sibling of the map DIV? |
Hi, Starting from his code, I create the folium_legend.ipynb with some modifications that make it possible:
In the colors.ipynb file I tested some color palettes from the seaborn. There are several others... |
Hello! I would like to know if you found a method to make it because I would like to do as you :) |
Nothing yet? |
@ColinTalbert Thank you for the excellent example. I was able to make it work. However, I noticed that when the map is in fullscreen mode, the legend disappears. Is there a solution for this? |
Thank you for this Colin! |
You really helped a lot of people with this! 😄 |
The position of the legend should be in "window coordinates", similar to mpl.legend(loc='upper left')
The text was updated successfully, but these errors were encountered: