Folium - Label each area in a GeoJSON - python

I have a GeoJSON file that contains country/empire borders in the year 1800. I am trying to depict this using Folium. I would like to have the name of each country/empire to be displayed (text) within each country/empire region in the map (basically the same as what is shown on "normal" present-day maps, but I can't just overlay this over present-day maps because the borders have changed over time). The GeoJSON file has a field that contains the name of each respective country/border, but I don't know how to display this on the map.
I do not want this to be a tooltip / popup. Instead, I want the text displayed in the base map, viewable without hovering your mouse over the map or clicking on anything.
I have seen some similar questions, and the responses are essentially to use a DivIcon. But this seems like a clunky solution and not very clean when dealing with a large map and many, many labels.
Is there a good way to display this? I looked in the Folium documentation and didn't see any arguments in the GeoJson class that seems like it would do this. (For example, it would be nice to tell Folium to automatically add 'region' labels, and to use a given field as the source of the labels).
Thanks!

Related

Alternative to plotly scatter_geo for skymap instead of Earth

I'm wanting to create an animated and interactive skymap using plotly, and I like scatter_geo (https://plotly.com/python-api-reference/generated/plotly.express.scatter_geo) but it seems to only allow for the use with the Earth (hence "geo"). I like the functionality in general of scatter_geo, I just need to be able to map it to sky coordinates (because mapping to Earth's lat/long gets annoying with the Earth's rotation) and to be able to have an image of my choosing as the backdrop (I'm wanting to use the Planck image of the MW). Oh, and it needs to be able to do the Mollweide projection.
I'm basically trying to create something like this animation of Fast Radio Bursts (https://vimeo.com/146295242) but with different data and with more interactivity, and I think plotly has the features I need for that - if I can map my sources to the sky, that is!
Does anyone know either how to make scatter_geo do this, or an alternative I can use? I have not yet been able to find one. It doesn't need to be a plotly function, but I would prefer to stick with Python.
Cheers!
...not sure how you create the flashes, but for the rest have a look at EOmaps
It can do all the stuff that .scatter_geo does, it should be able to handle any projection that pyproj can handle and gives you a map in Mollweide projection if you want :-)
... and on top of that you can interact with the map and assign assign any callbacks you like.

Exporting functional Plotly sunburst plot

Does anyone know how I can export a plotly sunburst plot as a standalone file which can keep all the functionalities, like annotation while hovering and expansion by click?
Visualizing a hierarchical data with plotly.sunburst in python is a beautiful and beneficial way of presenting it in the best order, while you are able to hover the values to see the annotation, click on each parent section to collapse and expand the child values, etc.. But to present the visualized data (plot) independently without having to open the Python notebook, needs the plot to be saved and exported in a standalone file format that allows keeping every functionality available.
Does anyone know how to do this? and what file format can give this luxury to us?
Thanks
Saving to an HTML file will do the trick:
doc

Folium Multiple map overlays

I'm fairly new to folium so this might be a bit noobish but I'm currently trying to plot several heatmaps of different data-points and add the ability to switch between the heatmaps all on the same plot. So for example I have such:
# The base map
hmap = folium.Map(location=[38.908111, -77.008871], tiles="Stamen Terrain", zoom_start=12)
# And each layer
# Homicide
HeatMap(list(zip(crime_homicide.LATITUDE.values, crime_homicide.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Homicides').add_to(hmap))
# Robbery
HeatMap(list(zip(crime_robbery.LATITUDE.values, crime_robbery.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Robbery').add_to(hmap))
# Assault
HeatMap(list(zip(crime_assault.LATITUDE.values, crime_assault.LONGITUDE.values))).add_to(folium.FeatureGroup(name='Assault').add_to(hmap))
folium.LayerControl(collapsed=False).add_to(hmap)
folium.GeoJson(dc_shape).add_to(hmap)
I tried using folium's FeatureGroup functionality but it looks like thats only specific markers as opposed to whole maps. Is there a way to switch between different maps if they're all heatmaps?
Your code seems fine.
Try this -
hmap.add_child
Or you can try heatmapwithtime as well, specifying different metrics which you can adjust in realtime to see different heatmaps.
But,FeatureGroup() will not seem to work with HeatMapWithTime and adding layers directly to the heatmap results in multiple time sliders on the side when there should be only one (common) time slider for all added layers.
So if you want to have a single control you'll have to put all your data in a single geojson and use that.
Why do you add a feature group? If you want to be able to select which instance of HeatMapWithTime you want to display, you can add both add them to the map, and they should both turn up in layer control.
m = Map()
HeatMapWithTime(data1).add_to(m)
HeatMapWithTime(data2).add_to(m)
FYI, a feature group is meant to group items and display them together. The items themselves don't get added to the map directly. For example:
fg = FeatureGroup().add_to(m)
fg.add_child(Item1)
fg.add_child(Item2)
Also this is the link, might help you :)
https://python-visualization.github.io/folium/plugins.html

Add Label Annotations on Axes

I currently annotate my charts with the last value of each series by adding a Label and supplying my the name of corresponding range it's plotted on:
Label(
...
x=data.index.max(),
y=data.loc[data.index.max(), 'my_col'],
y_range_name='my_range'
...
)
Which gives me:
How do I move the labels so they are positioned on their respective axis?
Example:
Please note that my labels' y-positioning is off, so I need some help with that aspect too. I've tried tweaking the y_offset but this has not yielded any consistently good results.
My data are always numerical time series.
As of Bokeh 1.2 there is no built-in annotation or glyph that will display outside the central plot area. There is an open issue on GitHub that this is similar to that you can follow or comment on. For the time being, something like this would require making a custom extension

Python Visualization: Determining Data Attributes Visually by Mouse Hover and Click

I am rendering very large X,Y,Z scientific datasets in the lateral plane and would like to click on the results to obtain greater textual detail about the area chosen by mouse-click.
So, for example, imagine a scientific dataset which is rendered as a filled contour plot and there is a bright spot. I would like to click on that spot to obtain a link or other textual information.
This would necessitate having another plane that would provide attribute information.
I would be most appreciative for any direction that others have used in similar cases. I expect this problem has been solved previously so I would prefer not reinventing the wheel.
Sounds like you'll need to dive into matplotlib's event handling code. This allows you to take some action when you click on a piece of the plot, or press a key, or whatever. Here are the example programs on the matplotlib site that have to do with event handling.

Categories