Comment créer une simple page avec LaTeX ?

Exemples de comment créer une simple première page avec LaTeX [TOC] ### Installer LaTeX Si vous n'avez pas encore installé LaTeX sur votre ordinateur vous pouvez par exemple utiliser les distributions LaTeX [texlive](https://www.tug.org/texlive/) (pour GNU/Linux, Windows) ou [mactex](http://www.tug.org/mactex/) (pour mac). Une fois LaTeX installé sur votre ordinateur, il est courant également d'installer un éditeur de texte LaTeX, il en existe plusieurs [voir "Comparison of TeX edit

How to download a file (pdf, text,...) from a url using python ?

Example of how to download a file from a url using python using mpython module [urllib](https://docs.python.org/2/library/urllib.html) and urlretrieve function. [TOC] ### Using python 3 Let's take for example the file available on http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf: >>> import urllib.request >>> file_url = 'http://math.univ-toulouse.fr/~besse/Wikistat/pdf/st-intro.pdf' >>> file_output_name = 'file_downloaded.pdf' >>> urllib.request.urlre

How to get the number of lines in a text file using python ?

Examples of how to get the number of lines in a text file using python: [TOC] ### Example 1 First example using readlines() and len(): >>> f = open('data.txt', 'r') >>> text=f.readlines() >>> NumberOfLine = len(text) >>> NumberOfLine 10 ### Example 2 A second example using a loop for: f = open('data.txt', 'r') NumberOfLine = 0 for line in f: NumberOfLine += 1 print 'Nombre de lignes: ',NumberOfLine Note: when you reach the end of the

Comment créer une liste d'éléments à partir d'une chaîne de caractères dans un template sous django ?

Exemple de comment créer une liste d'éléments à partir d'une chaîne de caractères dans un template sous django [TOC] ### Introduction Prenons par exemple la table suivante (intitulée Note) dans le fichier models.py comprenant la ligne tags (models.TextField(): chaîne de caractères) class Note(models.Model): ... = ... ... = ... ... = ... tags = models.TextField() tags est utilisé pour stocker des labels séparés par un point virgule ; (i

Comment tracer un nombre complexe en python avec matplotlib ?

Un exemple de comment tracer un nombre complexe en python avec matplotlib [TOC] ### Tracer un nombre complexe Soit le nombre complexe suivant z1 = 4 + 2i [image:plot-complex-number-geometric-representation-01 size:50 caption:Comment tracer un nombre complexe en python avec matplotlib ?] Exemple de comment créer une fonction avec python pour tracer un nombre complexeL import matplotlib.pyplot as plt import numpy as np import math z1 = 4.0 + 2.*1j x_min = -5.0

How to plot a complex number in python using matplotlib ?

An example of how to plot a complex number in python using matplotlib: [TOC] ### Plot a complex number Let's consider the following complex number z1 = 4 + 2i [image:plot-complex-number-geometric-representation-01 size:50 caption:How to plot a complex number in python using matplotlib ?] Example of how to create a python function to plot a geometric representation of a complex number: import matplotlib.pyplot as plt import numpy as np import math z1 = 4.0 + 2.*1

How to create a list of items from a string in a Django template ?

Example of how to split a string in a Django template [TOC] ### Introduction Let's consider the following example: with a table in models.py file with a row called tags (which is a string: models.TextField() ): class Note(models.Model): ... = ... ... = ... ... = ... tags = models.TextField() and tags is used to store labels separated by a semicolon ; (i.e. for example: New_York; Travel; Photo). The goal is to split the string tags i

How to create a simple maintenance page for a Django website (with Nginx, Gunicorn) on Ubuntu (digital ocean) ?

Simple example of how to create a python script to redirect on a maintenance page [TOC] Replace site_name by the website domain name ### Create a simple html page Go under the directory: cd /var/www/ Create the folder site_name_maintenance_page mkdir site_name_maintenance_page and then: mkdir html the path should be: /var/www/site_name_maintenance_page/html/ And create the file index.html nano index.html with the following lines <html> <head>

Comment utiliser un environnement conda dans un jupyter notebook ?

Exemple de comment utiliser un environnement conda dans un jupyter notebook ? [TOC] ### Créer un environnement conda Pour installer conda, aller sur la page [Anaconda Individual Edition](https://www.anaconda.com/distribution/). Une fois conda installé pour créer un environnement (intitulé par exemple ici worklab) on peut alors utiliser la commande suivante: conda create --name worklab et entrer source activate worklab pour activer l'environnement worklab. Note:

How to use a specific python conda environment in a Jupyter notebook ?

Example of how to use a given python conda environment in a Jupyter notebook. [TOC] ### Create a conda environment To install conda go to [Anaconda Individual Edition](https://www.anaconda.com/distribution/) . Since conda is installed in your system to create an environment (called for example hereafter worklab) use the following command: conda create --name worklab and then enter source activate worklab to activate the environment called worklab. Note: to get a

How to iterate over rows of a pandas data frame in python ?

To iterate over rows of a pandas data frame in python, a solution is to use [iterrows()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html), [items()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.items.html#pandas.DataFrame.items) or [itertuples()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples): [TOC] Let's consider the following pandas data fram

Comment itérer sur les lignes d'un tableau de données (ou data frame) avec pandas ?

Pour itérer sur l'ensemble des lignes d'une data frame avec pandas on peut utiliser [iterrows()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html), [items()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.items.html#pandas.DataFrame.items) ou encore [itertuples()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples): [TOC] Prenons par exemple la data fra

How to sum a given row of a data array in python with numpy ?

Example of how to sum a given row of a data array in python with numpy: [TOC] ### Sum a row of data Lets consider the following array: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55,

How to sum a given column of a data array in python with numpy ?

Example of how to sum a column of data in python with numpy: [TOC] ### Sum a given column of data Let's consider the following matrix >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55,

Comment additionner une colonne d'un tableau de données avec numpy ?

Exemple de comment additionner les nombres sur une colonne d'un tableau de données en python avec numpy: [TOC] ### Additionner les nombres avec sum() Soit la matrice suivante: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47

Comment additionner une ligne d'un tableau de données en python avec numpy ?

Exemple de comment additionner les nombres sur une ligne d'un tableau de données en python avec numpy: [TOC] ### Additionner les nombres avec sum() Soit la matrice suivante: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47,

Comment sélectionner aléatoirement les éléments d'une matrice en python avec numpy ?

Exemples de comment sélectionner aléatoirement les éléments d'une matrice en python avec numpy [TOC] ### Échantillonnage aléatoire avec choice() Soit une matrice 1D avec 10 éléments: >>> import numpy as np >>> data = np.arange(10) >>> data array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) \begin{equation} A = \left( \begin{array}{ccc} 0 & 1& 2& 3& 4& 5& 6& 7& 8& 9 \end{array}\right) \end{equation} Pour sélectionner aléatoirement n éléments de la matrice on peut utiliser la fon

How to randomly select elements of an array with numpy in python ?

Examples of how to randomly select elements of an array with numpy in python: [TOC] ### Randomly select elements of a 1D array using choice() Lets create a simple 1D array with 10 elements: >>> import numpy as np >>> data = np.arange(10) >>> data array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) \begin{equation} A = \left( \begin{array}{ccc} 0 & 1& 2& 3& 4& 5& 6& 7& 8& 9 \end{array}\right) \end{equation} To select randomly n elements, a solution is to use [choice()](https://do

How to randomly select rows of an array in python with numpy ?

Examples of how to randomly select rows of an array in python with numpy: [TOC] ### Create an array with numpy Let create the following array: >>> import numpy as np >>> data = np.arange(80).reshape((8, 10)) >>> data array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51,

How to upload from ICARE a MODIS L2 granule using ftp and python 3 ?

Example of how to upload with ftp a MODIS granule from ICARE server to my local machine in python 3. [TOC] Import python modules from ftplib import FTP from datetime import date import numpy as np import calendar Granule date & time year = 2008 month = 1 day = 8 hour = 14 minutes = 20 Retrieve count of days: d = date(year, month, day) - date(year, 1, 1) File name file = 'MYD06_L2.A{:04d}{:03d}.{:02d}{:02d}'.format(year,d.days+1,hour,minutes) P

How to change the color background of a matplotlib figure ?

Examples of how to change the color background of a matplotlib figure: [TOC] ### Using subplot and the option axisbg from pylab import * subplot(111, axisbg='#ababab') t = arange(0.0, 2.0, 0.01) s = sin(2*pi*t) plot(t, s) xlabel('x') ylabel('y') title('How to change background color in matplotlib ?') savefig('exemple_01.png') show() [image:376 size:50 caption:How to change the color background of a matplotlib figure ?] source: [How to set opacity of backgroun

Elementary operations on lists in python

Examples of elementary operations on lists in python [TOC] ### Create a list Create an empty list >>> l = [] >>> type(myfirstlist) <type 'list'> Create a list with 3 elements >>> l = ['a','b','c'] Note: in a list the elements can have different typrs >>> l = ['hello',1,2] ### Get the number of elements in a list To get the number of elements in a list a solution is to use len() >>> l = ['Pierre', 'Paul', 'Mathieu'] >>> len(l) 3

How to sum / add two or several matrices together in python ?

Examples of how to sum / add two or several matrices together in python using numpy: [TOC] ### Add two matrices of same size Let sum two matrices of same size. Let's consider the matrix A: >>> import numpy as np >>> A = np.array(([3,1],[6,4])) >>> A array([[3, 1], [6, 4]]) \begin{equation} A = \left( \begin{array}{ccc} 3 & 1 \\\\ 6 & 4 \end{array}\right) \end{equation} and matrix B: >>> B = np.array(([1,8],[4,2])) >>> B array([[1, 8], [4, 2]]

Comment additionner / sommer deux ou plusieurs matrices en python ?

Exemples de comment additionner (sommer) deux ou plusieurs matrices en python: [TOC] ### Additionner deux matrices de même taille Le cas le plus simple est si deux matrices A et B ont les mêmes dimensions. Soit par exemple la matrice A suivante: >>> import numpy as np >>> A = np.array(([3,1],[6,4])) >>> A array([[3, 1], [6, 4]]) \begin{equation} A = \left( \begin{array}{ccc} 3 & 1 \\\\ 6 & 4 \end{array}\right) \end{equation} et la matrice B: >>> B = np

How to get a complex number in polar coordinates in python ?

Examples of how to get a complex number in polar coordinates in python: [TOC] ### Using the python module cmath To get a complex number in polar coordinate a solution is to use the python module [cmath](https://docs.python.org/2/library/cmath.html): >>> import cmath Let's consider the following complex number: >>> z = 2 + 3j to get the polar coordinates: >>> r,theta = cmath.polar(z) >>> r 3.605551275463989 >>> theta 0.982793723247329 Note: to return the c