issue161:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue161:python [2020/09/27 14:30] – créée auntiee | issue161:python [2020/10/01 17:07] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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' | + | **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' |
- | So let me tell you about Folium. Directly from their website: | + | Bon, je l' |
+ | |||
+ | **So let me tell you about Folium. Directly from their website: | ||
" | " | ||
The library has a number of built-in tilesets from OpenStreetMap, | The library has a number of built-in tilesets from OpenStreetMap, | ||
- | 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" | + | 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" |
+ | |||
+ | Aussi, permettez que je vous parle de Folium. En direct de leur site Web : | ||
+ | « Folium facilite la visualisation, | ||
+ | |||
+ | « La bibliothèque a un certain nombre de jeux de tuiles intégrés pour OpenStreetMap, | ||
+ | |||
+ | Quand Halvard m'a envoyé le lien, j'ai repris mon Jupyter avec réticence et j'ai essayé l' | ||
- | When I got back from the Doctor' | + | **When I got back from the Doctor' |
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: | 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: | ||
Ligne 14: | Ligne 23: | ||
$ pip install folium | $ pip install folium | ||
- | You can also download the source code from the github repository at https:// | + | You can also download the source code from the github repository at https:// |
- | The idea is to provide various types of maps via the leaflet.js library. | + | Quand je suis rentré du cabinet médical, j'ai effectué une recherche à demi-intelligente sur le Web sur une façon d' |
+ | |||
+ | En premier lieu, vous devez installer la bibliothèque Folium (comme si vous n'avez pas prévu de le faire). La version actuelle est la 0.11.0. C'est facile en utilisant pip : | ||
+ | |||
+ | $ pip install folium | ||
+ | |||
+ | Vous pouvez aussi télécharger le code source à partir du dépôt github à : https:// | ||
+ | |||
+ | **The idea is to provide various types of maps via the leaflet.js library. | ||
Let's get started. | Let's get started. | ||
Ligne 28: | Ligne 45: | ||
m | m | ||
- | Once you run this program, you should see something like the image on the previous page. | + | Once you run this program, you should see something like the image on the previous page.** |
+ | L' | ||
+ | Commençons. | ||
- | Of course, I zoomed in a little bit, but there it is. | + | J'ai vécu pendant des années dans une petite municipalité appelée Canyon Lake, au Texas. C' |
+ | |||
+ | import folium | ||
+ | |||
+ | m = folium.Map(location=[29.8752, | ||
+ | |||
+ | m | ||
+ | |||
+ | Une fois que le programme est lancé, vous devriez voir quelque chose comme l' | ||
+ | |||
+ | **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 " | The code is pretty self-explanatory. If you are a purist like me, you can run it in " | ||
Ligne 38: | Ligne 67: | ||
Again, the code is fairly clear and easy to understand, but I'll break it down a bit. | 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. | + | 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.** |
+ | |||
+ | Bien sûr, j'ai agrandi un peu mais c'est ça. | ||
+ | |||
+ | Le code se comprend de lui-même. Si vous êtes un puriste comme moi, vous pouvez le lancer dans un Python « normal » ; c'est aussi facile, une fois que vous connaissez le truc. En haut à droite, voici le code révisé. | ||
+ | |||
+ | À nouveau, le code est bien clair et facile à comprendre, mais je vais le détailler un peu. | ||
+ | |||
+ | D' | ||
- | 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: | + | **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, | m = folium.Map(location=[29.8752, | ||
Ligne 53: | Ligne 90: | ||
zoom_start=13) | zoom_start=13) | ||
- | This will give you a somewhat nicer map in my opinion (below). | + | 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/ | + | La sortie est vraiment jolie et la carte est facile à agrandir et à réduire. Mais que se passe-t-il si nous voulons qu' |
+ | |||
+ | m = folium.Map(location=[29.8752, | ||
+ | |||
+ | Sauvegardez encore et relancez le programme. Il devrait ressembler à ceci : | ||
+ | |||
+ | Vraiment, c'est super facile ! | ||
+ | |||
+ | Nous pouvons aussi définir le type de carte en tuiles que folium utilisera. C'est un simple ajout sur la ligne d' | ||
+ | |||
+ | m = folium.Map(location=[29.8752, | ||
+ | |||
+ | Vous obtiendrez une carte encore un peu plus jolie, à mon avis (ci-dessous). | ||
+ | |||
+ | **If you want to enhance the map, you can easily add markers at a specific latitude/ | ||
Save and re-run the program and you’ll see something like the image bottom 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. | + | 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: | + | Si vous voulez améliorer la carte, vous pouvez facilement ajouter des marqueurs à une latitude/ |
+ | |||
+ | Enregistrez et relancez le programme ; vous verrez quelque chose comme l' | ||
+ | |||
+ | Je n'ai inclus qu'une petite partie de la sortie réelle de la carte. Si vous cliquez sur un des marqueurs, vous verrez quelque chose comme ce qui est montré à droite. | ||
+ | |||
+ | **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()) | m.add_child(folium.LatLngPopup()) | ||
Ligne 67: | Ligne 124: | ||
When you save and run the program again, you should get something like that shown on the next page, top left. | 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:// | + | 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:// |
+ | |||
+ | Comme nous pouvons le dire en France : « Hé ! Attendez : il y en a encore plus ! » Ne serait-ce pas sympa d' | ||
+ | |||
+ | m.add_child(folium.LatLngPopup()) | ||
+ | |||
+ | Quand vous sauvegardez et relancez le programme, vous devriez obtenir quelque chose comme ce qui est présenté à la page suivante, en haut à gauche. | ||
+ | |||
+ | À nouveau, j'ai agrandi pas mal. Il y a plein d' | ||
- | 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:// | + | **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:// |
“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.” | “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. | + | 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: | + | Maintenant, nous commençons l' |
+ | |||
+ | « L' | ||
+ | |||
+ | En haut à droite, voici un exemple tiré de ce site Web. | ||
+ | |||
+ | **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 folium | ||
Ligne 87: | Ligne 158: | ||
m = folium.Map(location=[48, | m = folium.Map(location=[48, | ||
- | Next, we have to define the parameters for the Choropleth portion of folium (shown above). | + | Next, we have to define the parameters for the Choropleth portion of folium (shown above).** |
- | And add it to the map object: | + | Faisons-le nous-mêmes avec une démo tirée du site Web de Folium. Nous aurons besoin d' |
+ | |||
+ | import folium | ||
+ | import pandas as pd | ||
+ | import webbrowser | ||
+ | |||
+ | Maintenant, nous devons paramétrer une adresse url qui pointe sur les données, régler quelques autres variables (voir ci-dessous) et demander à pandas de les charger. | ||
+ | |||
+ | Ensuite, nous créons la carte folium comme nous l' | ||
+ | |||
+ | m = folium.Map(location=[48, | ||
+ | |||
+ | Puis nous devons définir les paramètres de la portion Choropleth de folium (présenté ci-dessus). | ||
+ | |||
+ | **And add it to the map object: | ||
folium.LayerControl().add_to(m) | folium.LayerControl().add_to(m) | ||
Ligne 101: | Ligne 186: | ||
webbrowser.open(output_file, | webbrowser.open(output_file, | ||
- | When you run it, it will produce the image shown below. | + | 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, | + | Et ajoutez-la à l' |
+ | |||
+ | folium.LayerControl().add_to(m) | ||
+ | |||
+ | Comme précédemment, | ||
+ | |||
+ | output_file = " | ||
+ | |||
+ | m.save(output_file) | ||
+ | |||
+ | webbrowser.open(output_file, | ||
+ | |||
+ | Quand vous le lancerez, il produira l' | ||
+ | |||
+ | **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, | ||
I’ve put the source code for this month up on Pastebin. The links are: | I’ve put the source code for this month up on Pastebin. The links are: | ||
Ligne 111: | Ligne 210: | ||
- | As always, until next time; stay safe, healthy, positive and creative! | + | As always, until next time; stay safe, healthy, positive and creative!** |
+ | |||
+ | Bon ! C'est tout pour cette fois-ci. Je vous suggère vivement d' | ||
+ | |||
+ | J'ai mis le code source de ce mois sur pastebin. Les liens sont : | ||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | Comme toujours, à la prochaine fois ; soyez prudent, en bonne santé, positif et créatif ! |
issue161/python.1601209858.txt.gz · Dernière modification : 2020/09/27 14:30 de auntiee