issue161:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue161:python [2020/09/29 15:28] – d52fr | 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' | ||
- | Bon, je l' | + | Bon, je l' |
**So let me tell you about Folium. Directly from their website: | **So let me tell you about Folium. Directly from their website: | ||
Ligne 11: | Ligne 11: | ||
Aussi, permettez que je vous parle de Folium. En direct de leur site Web : | Aussi, permettez que je vous parle de Folium. En direct de leur site Web : | ||
- | « folium | + | « Folium |
- | La bibliothèque a un certain nombre de jeux de tuiles intégrés pour OpenStreetMap, | + | « 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' | + | 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' | ||
Ligne 25: | Ligne 25: | ||
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:// | ||
- | Quand je suis rentré du cabinet médical, j'ai effectué une recherché | + | Quand je suis rentré du cabinet médical, j'ai effectué une recherche |
- | En premier lieu, vous devez installer la bibliothèque Folium (comme si vous ne l'avez pas vu arriver). La version actuelle est la 0.11.0. C'est facile en utilisant pip : | + | 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 | $ pip install folium | ||
Ligne 51: | Ligne 51: | ||
Commençons. | Commençons. | ||
- | J'ai vécu pendant des années dans une petite | + | J'ai vécu pendant des années dans une petite |
import folium | import folium | ||
Ligne 71: | Ligne 71: | ||
Bien sûr, j'ai agrandi un peu mais c'est ça. | 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é. | + | 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. | À nouveau, le code est bien clair et facile à comprendre, mais je vais le détailler un peu. | ||
- | D' | + | 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: | ||
Ligne 92: | Ligne 92: | ||
This will give you a somewhat nicer map in my opinion (below).** | This will give you a somewhat nicer map in my opinion (below).** | ||
- | La sortie est vraiment jolie et la carte est facile à engrandir | + | La sortie est vraiment jolie et la carte est facile à agrandir |
m = folium.Map(location=[29.8752, | m = folium.Map(location=[29.8752, | ||
Ligne 100: | Ligne 100: | ||
Vraiment, c'est super facile ! | Vraiment, c'est super facile ! | ||
- | Nous pouvons aussi définir le type de carte en tuiles | + | Nous pouvons aussi définir le type de carte en tuiles |
m = folium.Map(location=[29.8752, | m = folium.Map(location=[29.8752, | ||
Ligne 111: | Ligne 111: | ||
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.** | ||
+ | |||
+ | 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: | **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: | ||
Ligne 119: | Ligne 125: | ||
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:// | ||
Ligne 125: | Ligne 139: | ||
Top right is an example image from that website.** | Top right is an example image from that website.** | ||
+ | |||
+ | 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: | **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: | ||
Ligne 139: | Ligne 159: | ||
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).** | ||
+ | |||
+ | 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: | **And add it to the map object: | ||
Ligne 153: | Ligne 187: | ||
When you run it, it will produce the image shown below.** | When you run it, it will produce the image shown below.** | ||
+ | |||
+ | 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, | **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, | ||
Ligne 164: | Ligne 212: | ||
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.1601386139.txt.gz · Dernière modification : 2020/09/29 15:28 de d52fr