How to add a grid on a figure in matplotlib ?

Examples of how to add a grid on a figure in matplotlib [TOC] ### Using grid() function To add a grid in the background on a figure in matplotlib, a solution is to use the pyplot function [grid()](http://matplotlib.org/api/pyplot_api.html) pour connaitre l'ensemble des arguments possibles). Example: [image:454 size:50 caption:How to add a grid on a figure in matplotlib ?] import matplotlib.pyplot as plt import numpy as np x = np.arange(-5,5, .01) y = np.sin(2*np.pi*x)

How to change imshow axis values (labels) in matplotlib ?

Examples of how to change imshow axis values (labels) in matplotlib: [TOC] Let's consider a simple figure using matplotlib imshow import numpy as np import matplotlib.pyplot as plt def f(x,y): return (x+y)*np.exp(-5.0*(x**2+y**2)) x,y = np.mgrid[-1:1:100j, -1:1:100j] z = f(x,y) plt.imshow(z) plt.colorbar() plt.title('How to change imshow axis values with matplotlib ?', fontsize=8) plt.savefig("imshow_change_values_on_axis_01.png", bbox_inches='tight')

Comment changer les valeurs sur les axes d'une figure imshow sous matplotlib ?

Exemples de comment changer les valeurs sur les axes d'une figure imshow sous matplotlib [TOC] Soit une simple figure tracée en utilisant la fonction imshow de matplotlib: import numpy as np import matplotlib.pyplot as plt def f(x,y): return (x+y)*np.exp(-5.0*(x**2+y**2)) x,y = np.mgrid[-1:1:100j, -1:1:100j] z = f(x,y) plt.imshow(z) plt.colorbar() plt.title('How to change imshow axis values with matplotlib ?', fontsize=8) plt.savefig("imshow_change_valu

How to get a list of files located in a folder using python ?

Examples of how to get a list of files located in a folder using python: [TOC] ### Using listdir() function Lets assume a folder that contains: dir01/ dir02/ fich01.txt fich02.txt fich02.txt To get a list of files located in the current folder using python a first solution is to use [os.listdir()](https://docs.python.org/2/library/os.html#os.listdir): >>> import os >>> list = os.listdir('.') >>> print(list) [dir01,dir02,fich01.txt,fich

How to download a web pdf file from its url in python ?

To download a pdf from a given web url using python, a solution is to use the module [urllib](https://docs.python.org/2/library/urllib.html). Lets try to download the file available from the following url http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf using urlretrieve: >>> import urllib >>> urllib.urlretrieve('http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf', "st-intro.pdf") Note: it is also possible to directly change the name of the file (

How to create a list of files with a given extension (.txt, .avi, .docx, etc) in python ?

Example of how to create a list of files with a given extension in python. Lets assume we want to create a list of all the files with the extensions '.hdf' in a folder called 'output': ./output/ MYD06_L2.A2014038.2300.061.2018052102433.hdf MYD06_L2.A2014033.1930.061.2018052042944.hdf MYD06_L2.A2014046.2210.061.2018052112538.hdf MYD06_L2.A2014058.2235.061.2018052141003.hdf MYD06_L2.A2014037.2045.061.2018052094723.hdf MYD06_L2.A2014054.2125.061.2018052131042.hdf MYD06_L2.A

Comment créer une liste des fichiers d'un dossier se terminant avec une extension donnée (.txt, .avi, .docx, etc) sous python ?

Exemple de cas: on veut obtenir une liste de tous les fichiers dans le dossier 'output' se terminant par '.hdf': ./output/ MYD06_L2.A2014038.2300.061.2018052102433.hdf MYD06_L2.A2014033.1930.061.2018052042944.hdf MYD06_L2.A2014046.2210.061.2018052112538.hdf MYD06_L2.A2014058.2235.061.2018052141003.hdf MYD06_L2.A2014037.2045.061.2018052094723.hdf MYD06_L2.A2014054.2125.061.2018052131042.hdf MYD06_L2.A2014034.2015.061.2018052062330.hdf MYD06_L2.A2014059.2010.061.2018052

Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?

Exemple de comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib: [image:imshow-extent-custum-aspect-ratio-00 size:50 caption:Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?] import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data) plt.colorbar() plt.savefig("imshow_extent_custum_aspect_ratio_00.png", bbox_inches='tight

How to change imshow aspect ratio and fit the colorbar size in matplotlib ?

An example of how to change imshow aspect ratio and adjust the colorbar accordingly in matplotlib: [image:imshow-extent-custum-aspect-ratio-00 size:50 caption:Comment changer la forme d'une figure imshow et avoir la barre de couleurs de même taille avec matplotlib ?] import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data) plt.colorbar() plt.savefig("imshow_extent_custum_aspect_ratio_00.png", bbox_inches='tight') plt.close()

How to change imshow aspect ratio in matplotlib ?

Examples of how to change imshow aspect ratio in matplotlib: [TOC] Matplotlib imshow function can return figures with very elongated shapes, example: import numpy as np import matplotlib.pyplot as plt data = np.random.rand(50,1000) plt.imshow(data, extent=[-1,1,-10,10]) plt.savefig("imshow_extent_custum_aspect_ratio_01.png", bbox_inches='tight') plt.close() [image:imshow-extent-custum-aspect-ratio-01 size:30 caption:How to change imshow aspect ratio in matplotlib ?]

Comment supprimer les doublons (les répétitions) pour filtrer une requête avec Django ?

Pour supprimer les doublons (les répétitions) pour filtrer une requête avec Django, il existe la fonction [distinct()](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#distinct). Exemple, on a crée la table suivante qui sauvegarde l'adresse ip d'un visiteur, la page visitée et la date (voir [Comment afficher dans un template le nombre de visiteurs en ligne sous Django ?](https://www.science-emergence.com/Articles/Comment-afficher-dans-un-template-le-nombre-de-visiteurs-en-ligne-sous-

How to show in a template the number of online visitors with Django ?

An example of solution to display in a template the number of online visitors in a django web site: [TOC] ### Create a table Visitor_Infos First, create a table in models.py called for example: Visitor_Infos: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Edit/create the file processor.py Edit the context file processor.py: from django.conf

How to create a 2d histogram with matplotlib ?

Examples of how to create a 2d histogram with matplotlib [TOC] ### Using the matplotlib hist2d function To create a 2d histogram in python there are several solutions: for example there is the matplotlib function [hist2d](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist2d.html). from numpy import c_ import numpy as np import matplotlib.pyplot as plt import random n = 100000 x = np.random.standard_normal(n) y = 3.0 * x + 2.0 * np.random.standard_normal(n)

Comment afficher dans un template le nombre de visiteurs en ligne sous Django ?

Exemple de solution pour afficher dans un template le nombre de visiteurs en ligne pour un site développé avec Django [TOC] ### Sauvegarder les visiteurs dans une table On peut tout d'abord dans le fichier models.py ajouter la table suivante: class Visitor_Infos(models.Model): ip_address = models.GenericIPAddressField() page_visited = models.TextField() event_date = models.DateTimeField(default=datetime.now) ### Editer le fichier processor.py On peut ensuite éditer le

How to create a json file with the names and attributes of a VIIRS L2 CLDPROP netcdf file using python 3 ?

Example of python 3 code to save in a json file the names and attributes of a VIIRS L2 CLDPROP netcdf file. [TOC] Note: the json file created can be found [here](/Files/viirs-cldprop-l2-001-names-attributes/) ### Read the file import netCDF4 import numpy as np import json import pprint f = netCDF4.Dataset('CLDPROP_L2_VIIRS_SNPP.A2018046.1436.001.2019068074007.nc') ### Get all group names group_dic = f.groups.keys() ### Get names and attributes in each group

How to download a VIIRS L2 CLDPROP netcdf file from ladsweb using python ?

Example of python script to download on your local machine a VIIRS L2 CLDPROP netcdf granule file from [ladsweb](https://ladsweb.modaps.eosdis.nasa.gov/search/) using python. [TOC] Lets consider the following data for example: year = 2018 month = 2 day = 15 hour = 14 minute = 36 Note: VIIRS granules are every 6 minutes ### Convert day to count of days First step we need to get the count of days: from datetime import date d0 = date(year, 1, 1) d1 = date(year

How to get visitor ip address for a django project ?

Example of how to get visitor ip address for a django project: [TOC] ### Get visitor ip address def visitor_ip_address(request): x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') return ip ### Check if ip address is valid To check if the ip address is valid, a solution is to use the [socket](h

How to reverse columns order in a table with python ?

Example of how to reverse columns order in a table with python: let's consider the following table: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. & 9. & 7. &

Comment inverser l'ordre des colonnes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des colonnes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 1., 2., 3., 4., 5., 6., 7.], ... [6., 6., 4., 9., 7., 5., 4., 8.], ... [4., 9., 3., 7., 5., 2., 3., 1.], ... [6., 9., 8., 1., 4., 3., 2., 1.], ... [4., 8., 7., 6., 1., 7., 1., 1.]]) \begin{equation} \begin{array}{cccccc} 0. & 1. & 2. & 3. & 4. & 5. & 6. & 7.\\\\ 6. & 6. & 4. &

How to reverse rows order in a table with python ?

Examples of how to reverse rows order in a table with python. Let's consider a random table: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.], ... [9.,

Comment inverser l'ordre des lignes d'un tableau en python avec numpy ?

Exemples de comment inverser l'ordre des lignes d'un tableau en python avec numpy. Soit un tableau quelconque: >>> import numpy as np >>> table = np.array([[0., 4., 4., 2., 7., 0.], ... [1., 2., 7., 2., 3., 4.], ... [2., 2., 3., 3., 6., 3.], ... [3., 9., 0., 6., 3., 3.], ... [4., 3., 1., 8., 2., 5.], ... [5., 9., 3., 7., 6., 8.], ... [6., 9., 5., 8., 1., 8.], ... [7., 9., 6., 7., 3., 7.], ... [8., 2., 1., 4., 4., 8.],

How to find the minimum and maximum values in an array using numpy and python ?

Examples of how to find the minimum and maximum values in an array using numpy and python: [TOC] ### Approach 1: To find the minimum and maximum values in an array, a solution is to use the numpy functions max() and min(): >>> import numpy as np >>> A = np.array(([2,7,-1],[1,9,8],[-1,5,6])) >>> A array([[ 2, 7, -1], [ 1, 9, 8], [-1, 5, 6]]) >>> np.min(A) -1 >>> np.max(A) 9 ### Approach 2: Another solutio

How to rotate an image using python ?

Examples of how to rotate an image using python: [TOC] ### Rotate an image and keep the same size as the original image To rotate an image, a solution is to use the pillow function [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:How to rotate an image using python ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rotate(45) print(i

Comment tourner une image en python avec le module pillow ?

Exemples de comment tourner une image en python avec le module pillow [TOC] ### Tourner une image en gardant la même taille Pour tourner une image, il existe la fonction pillow [rotate()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), exemple [images:eiffel-tower;eiffel-tower-rotate-01 dim:1*2 size:75 caption:Comment tourner une image en python avec le module pillow ?] from PIL import Image im = Image.open("eiffel_tower.jpg") print(im.size) im = im.rota

Comment retourner une image horizontalement en python ?

Exemples de comment retourner une image horizontalement en python [TOC] ### Obtenir une image retournée horizontalement avec pillow Pour retourner une image horizontalement en python, il existe la fonction pillow [mirror()](https://pillow.readthedocs.io/en/3.1.x/reference/Image.html), example: from PIL import Image from PIL import ImageOps im = Image.open("lena.png") im = ImageOps.mirror(im) im.save("lena_mirror.png") im.show() [images:458;lena-mirror dim:1*2 size