Outils pour utilisateurs

Outils du site


issue161:python

Ceci est une ancienne révision du document !


Alright, I admit it. I let things slide on my FCM duties this month (and other things as well, but that's another story for another time). Between the medical issues, and trying to write chapters for the book, it just never happened. Thankfully, my friend in Norway, Halvard, came to my rescue once again. A few weeks ago, he sent me a message about a neat visualization tool for Python called Folium. I thanked him and took a look. I was really impressed until I realized that all the examples that I was seeing were for Jupyter Notebook. I spent a little bit of time looking for examples that didn't use Jupyter Notebook, but since I didn't put much effort into it, I really couldn't find anything. Today, just before I needed to leave to go to a Doctor's appointment, I received Ronnie's monthly gentle nudge about the deadline for articles. Rolling my eyes like a 14 year-old girl, and sighing deeply, I sent myself a reminder that when I got back home, I REALLY needed to get to work on this.

So let me tell you about Folium. Directly from their website: “folium makes it easy to visualize data that’s been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map. The library has a number of built-in tilesets from OpenStreetMap, Mapbox, and Stamen, and supports custom tilesets with Mapbox or Cloudmade API keys. folium supports both Image, Video, GeoJSON and TopoJSON overlays.” When Halvard sent me the link, I reluctantly pulled up Jupyter and tried the sample code. I could see a number of things that I'd like to try to do with this really neat tool. But again, things got in my way, and I left it as a “to look at in the future”. The biggest thing that kept me from digging any further at the time was the need for Jupyter Notebook. I don't know why I dislike it so very badly. I just do.

When I got back from the Doctor's office, I did a semi-intelligent web search for a way to use folium without needing to use JN. Sure enough, there were a number of links that gave me a starting point. So may I present my findings. First of all, you have to install the Folium library (as if you didn't see that coming). The current version is 0.11.0. It's easy by using Pip: $ pip install folium You can also download the source code from the github repository at https://github.com/python-visualization/folium. The repository has a large number of examples that you can try out, but I'm guessing that they are all run under Notebook, since the extensions are all “ipynb”. That's for another day.

The idea is to provide various types of maps via the leaflet.js library. Let's get started. I lived in a small community called Canyon Lake, Texas for many years. It was a beautiful area and that time was a very happy one for myself and my family, so we will use that location for the first demonstration. Assuming that you want to do this directly from Jupyter Notebooks, it is really easy: import folium m = folium.Map(location=[29.8752, -98.2625]) m Once you run this program, you should see something like the image on the previous page.

Of course, I zoomed in a little bit, but there it is. The code is pretty self-explanatory. If you are a purist like me, you can run it in “normal” Python, that's pretty easy as well, once you know the trick. Top right is the revised code. Again, the code is fairly clear and easy to understand, but I'll break it down a bit. First, we import both folium and webbrowser libraries. Then we create an instance of the map by calling folium.Map with the Lat and Lon as a list. We then define the output filename and call the .save method. Finally, we use the webbrowser library to open the file in a new window.

The output is really nice, and the map is easy to zoom into and out of. But what if we want to have it already zoomed in for us. That’s also easy. Just change the m = folium.Map… line to: m = folium.Map(location=[29.8752, -98.2625], zoom_start=13) And save and re-run the program. It should look something like this: Again, super easy! We can also define the type of tile maps that folium will use. It’s a simple addition to the object initialization line. Change the m= folium.Map line to the following: m = folium.Map(location=[29.8752, -98.2625], tiles='Stamen Terrain', zoom_start=13) This will give you a somewhat nicer map in my opinion (below).

If you want to enhance the map, you can easily add markers at a specific latitude/longitude. Again, it’s very easy. Just add the following lines before the line that assigns the filename, but after you’ve created the folium map. In this case, I’m going to add three markers (top right) Save and re-run the program and you’ll see something like the image bottom right. I’ve included just a small selection of the actual map output. If you click on one of the markers you will see something like that shown right.

As they say here in the U.S., “BUT WAIT! There’s More!”. Wouldn’t it be nice to add the ability for the user (you and me in this case) to click the mouse on the map anywhere and get a Latitude and Longitude in a pop-up? That’s yet again, another easy addition. Simply add the following line, before the output filename assignment: m.add_child(folium.LatLngPopup()) When you save and run the program again, you should get something like that shown on the next page, top left. Again, I’ve zoomed in pretty deeply. There are plenty of other types of maps like these that you can use. You can check the documentation at https://python-visualization.github.io/folium/index.html .

Now we start to explore one of the other types of maps that folium can produce, a Choropleth map. According to the Data Visualization Catalog website (https://datavizcatalogue.com/methods/choropleth.html): “Choropleth Maps display divided geographical areas or regions that are coloured, shaded or patterned in relation to a data variable. This provides a way to visualise values over a geographical area, which can show variation or patterns across the displayed location.” Top right is an example image from that website.

Let’s do it for ourselves with a demo from the folium website. First, we will need to import folium, webbrowser, and for this one, pandas: import folium import pandas as pd import webbrowser Now, we need to set a url address that points to the data, set a few other variables (shown below), and have pandas load it. Next, we create the folium map as we did before: m = folium.Map(location=[48, -102], zoom_start=3) Next, we have to define the parameters for the Choropleth portion of folium (shown above).

And add it to the map object: folium.LayerControl().add_to(m) As before, you simply assign the output filename and save the html file, then send it to webbrowser. output_file = “map2.html” m.save(output_file) webbrowser.open(output_file, new=2) When you run it, it will produce the image shown below.

Well, that’s about it for this time. I strongly suggest that you try the examples from folium and others from the web to see how folium can help you in your own programming. There was a number of examples in a jupyter notebook gallery, but sometime between when I started and as I’m ending, many of them have disappeared, and, when checking the folium version on the gallery site, it shows a higher number than what’s available on the repository, so expect a new version out soon. I’ve put the source code for this month up on Pastebin. The links are: https://pastebin.com/Fv5jVy3G https://pastebin.com/y1vE6z1A As always, until next time; stay safe, healthy, positive and creative!

issue161/python.1601299534.txt.gz · Dernière modification : 2020/09/28 15:25 de d52fr