Comment masquer les valeurs d'une matrice depuis une autre matrice sous python ?

Exemples de comment sélectionner ou masquer les valeurs d'une matrice d'après les valeurs d'une autre matrice sous python: [TOC] ### Masquer une matrice à partir d'une autre matrice Pour masquer une matrice à partir d'une autre matrice, il existe plusieurs possibilités avec numpy (voir le module intitulé [ma](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html)). On peut par exemple utiliser [numpy.ma.masked_where](https://docs.scipy.org/doc/numpy-1.13.0/reference/genera

How to save a dictionary in a json file with python ?

Examples of how to save a dictionary in a json (JavaScript Object Notation) file with python [TOC] See also: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) and [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Save a python dictionary in a json file To save a dictionary in python to a json file, a solution is to use the json function [dump()](https://docs.python.org/

Comment sauvegarder un dictionnaire dans un fichier json avec python ?

Exemples de comment sauvegarder un dictionnaire dans un fichier json (JavaScript Object Notation) avec python: [TOC] Voir aussi: [pickle — Python object serialization](https://docs.python.org/3/library/pickle.html#module-pickle) et [marshal — Internal Python object serialization](https://docs.python.org/3/library/marshal.html#module-marshal) ### Sauvegarder un dictionnaire dans un fichier json Pour sauvegarder un dictionnaire avec python dans un fichier json, on peut utiliser la fonc

How to plot points in front of a line in matplotlib ?

By default in matplotlib, if a line and a scatter plot are plotted in a same figure, the points are placed behind the line, illustration (no matter if the scatter() is called before plot()): [image:scatter-points-order-01 size:50 caption:How to plot points in front of a line in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8,9] y = [1,2,3,4,5,6,7,8,9] plt.plot([0,10],[0,10]) plt.scatter(x,y,s=300,color='red') plt.title("Tracer un nuage de points devant u

How to add a legend to a scatter plot in matplotlib ?

Examples of how to add a legend to a scatter plot in matplotlib: [TOC] ### Add a simple legend to a scatter plot Using the pyplot function [legend()](http://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend): [image:ScatterPlot-09 size:50 caption:How to add a legend for a scatter plot in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4] y = [4,1,3,6] plt.scatter(x, y, c='coral', label='Class 1') x = [5,6,7,8] y = [1,3,5,2] plt.scatt

How to increase the size of scatter points in matplotlib ?

Examples of how to increase the size of scatter points in matplotlib: [TOC] ### Increase the size of all points To increase the size of scatter points, a solution is to use the option "s" from the function [scatter()](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot), example [image:ScatterPlot-07 size:50 caption:How to increase the size of scatter points in matplotlib ?] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [4,1,3,6,1,3,5,2] plt.s

How to create a scatter plot with several colors in matplotlib ?

Examples of how to create a scatter plot with several colors in matplotlib: [TOC] ### Combining two scatter plots with different colors To change the color of a scatter point in matplotlib, there is the option "c" in the function [scatter](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot). First simple example that combine two scatter plots with different colors: [image:ScatterPlot-05 size:50 caption:How to create a scatter plot with several colors in matplotlib ? ]

How to add a number to each element of a list in python ?

Examples of how to add a number to each element of a list in python: [TOC] ### Add a number to a list of integers Add +10 to each integer of a list using a "list comprehension": >>> l2 = [i+10 for i in l] >>> l2 [13, 11, 15, 18, 14] Increment a list of integer numbers: >>> l = [3,1,5,8,4] >>> l2 = [i+1 for i in l] >>> l2 [4, 2, 6, 9, 5] ### Add a number to a list of reals Increment a list of real numbers: >>> import numpy as np

How to create a simple scatter plot using matplotlib ?

Simples examples of how to create a simple scatter plot using matplotlib [TOC] ### Scatter plot with matplotlib To plot a scatter plot with matplotlib, ta solution is to use the method [scatter](http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot) from the class pyplot, example: [image:418 size:50 caption:How to create a simple scatter plot using matplotlib ? ] import matplotlib.pyplot as plt x = [1,2,3,4,5,6,7,8] y = [4,1,3,6,1,3,5,2] plt.scatter(x,y)

How to add a base64 encoded image in a html canvas using javascript ?

Example of how to add a base64 encoded image in a html canvas using javascript. We want to include the following image (the base64 encoded image can be found [here](/Files/base64/) in a html canvas: [image:550 size:25 caption:How to add a base64 encoded image in a html canvas using javascript ?] Source code <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body> <canvas id="t

Comment afficher dans un canvas html une image en base64 avec javascript ?

Exemple de comment afficher dans un canvas html une image en base64 avec javascript. On dispose de l'image suivante dont le code en base 64 est disponible [ici](/Files/base64/): [image:550 size:25 caption:Comment afficher dans un canvas html une image en base64 avec javascript ? ] Page html pour afficher l'image ci-dessus: <!DOCTYPE html> <html lang="en"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> </head> <body>

How to reverse the order of list elements in python ?

Examples of how to reverse a list in python: [TOC] ### Inverse a list with reverse() To modify a list by reversing the order of the elements, a solution is to use the function [reverse()](https://docs.python.org/2/tutorial/datastructures.html), example >>> l = ['a','b','c','d','e','f'] >>> l.reverse() >>> l ['f', 'e', 'd', 'c', 'b', 'a'] ### Inverse a list with reversed() To iterate through a list in the reversed order, a solution is to use the function [re

How to test locally the microphone in chrome using javascript ?

An example of how to test locally the microphone in chrome using javascript: [TOC] The primary goal here was to develop and test locally the microphone with chrome using a javascript application for speech recognition. The problem was that it is not possible to use the microphone locally for security reasons since an https is required to use it. Here a solution is presented to test locally the microphone in chrome using javascript and python: ### Install openssl and create a self-signed

How to copy an array (matrix) in python ?

Examples of how to copy an array in python: [TOC] ### Copy an array with the numpy copy() function To copy an array in python, a simple solution is to use the numpy function called [copy()](http://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html), example: >>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> y = np.copy(x) >>> y[1] = 7 >>> y array([1, 7, 3, 4]) >>> x array([1, 2, 3, 4]) This function will work for mos

How to copy a dictionary in python ?

To copy a dictionary in python, there are several approaches with different properties: [TOC] ### Copy a dictionary with deepcopy To get a copy completely independent of the first dictionary a solution is to use the function [deepcopy](https://docs.python.org/2/library/copy.html) >>> import copy >>> d1 = {'a':1} >>> d2 = copy.deepcopy(d1) >>> d2 {'a': 1} Here if the second dictionary d2 is modified the first dictionary will be not affected, examples:

Comment remplacer une colonne d'un tableau par la colonne d'un autre tableau en python ?

Exemples de comment remplacer (copier) une colonne d'un tableau par la colonne d'un autre tableau en python: [TOC] ### Tableau de même taille Cas ou les deux tableaux ont la même taille: exemple remplacer la colonne 1 du tableau N par la colonne 2 du tableau M: >>> import numpy as np >>> M = np.array([[2,7,1],[3,3,1],[5,4,2],[0,1,8]]) >>> M array([[2, 7, 1], [3, 3, 1], [5, 4, 2], [0, 1, 8]]) >>> N = np.zeros((4,6)) >>>

How to copy a list in python ?

To copy a list in python, a solution is to use the "built-in" function [list()](https://docs.python.org/2/library/functions.html#list), example: >>> L1 = ['a','b','c','d','e','f'] >>> L2 = list(L1) >>> L2 ['a', 'b', 'c', 'd', 'e', 'f'] Another solution is to use the list comprehension: >>> L1 = ['a','b','c','d','e','f'] >>> L2 = [i for i in L1] >>> L2 ['a', 'b', 'c', 'd', 'e', 'f'] **WARNING**: to copy a list do not use the = operator, because

Comment tester localement son microphone avec javascript et chrome ?

Un exemple de comment tester localement son microphone avec javascript et chrome: [TOC] L'objectif de départ était de pourvoir tester localement une application basée sur javascript utilisant le microphone pour reconnaitre du texte a partir de la voix (speech recognition). Le problème est qu'il n'est pas possible d'utiliser localement son microphone avec chrome directement, car il faut utiliser le https pour pouvoir utiliser le microphone. On va presenter ici une solution pour pouvoir test

How to get the size of a list in python ?

To get the size of a list in python, a solution is to use the python function [len](https://docs.python.org/2/library/functions.html#len), examples: >>> a = ['bob','ben','Tim'] >>> len(a) 3 another example with an empty list: >>> l = [] >>> len(l) 0 Another solution is to iterate in the list using a loop: >>> a = ['bob','ben','Tim'] >>> list_size = 0 >>> for i in a: ... list_size = list_size + 1 ... >>> list_size 3

How to find the smallest value in a list and its index with python ?

To find the smallest value in a list with python, a solution is to use the function [min()](https://www.tutorialspoint.com/python/list_min.htm): >>> l = [4,7,1,4,1,6,9] >>> min(l) 1 which returns 1 here. To find the index, there is the function [index()](https://www.tutorialspoint.com/python/list_index.htm), example: >>> l.index(min(l)) 2 Note: this function only returns the first index found. To find all the indexes of the smallest value (if there is several o

How to find the largest value in a list and its index with python ?

To find the largest number in a list with python, a solution is to use the function [max()](https://www.tutorialspoint.com/python/list_max.htm): >>> l = [4,7,9,4,1,6,9] >>> max(l) 9 which returns 9 here. To find the index, there is the function [index()](https://www.tutorialspoint.com/python/list_index.htm), example: >>> l.index(max(l)) 2 Note: this function only returns the first index found. To find all the indexes of a maximum value (if there is several occu

How to sort a list by the number of occurrences in python ?

Examples of how to sort a list by the number of occurrences (repetitions) in python [TOC] ### Sort a list of numbers by the number of occurrences: To sort a list of numbers by the number of occurrences, a solution is to use the function [counter](https://docs.python.org/2/library/collections.html#collections.Counter), example: >>> from collections import Counter >>> l = [8, 3, 9, 2, 7, 1, 3, 2, 0, 0, 7, 8, 6, 9, 4, 6, 3, 3, 2, 5] >>> l_sorted = Counter(l).most_common()

Trier une liste en fonction du nombre de répétitions (occurrences) avec python

Exemples de comment trier une liste en fonction du nombre de répétitions (occurrences) avec python [TOC] ### Trier une liste de nombres en fonction du nombre de répétitions Pour trier une liste de nombres en fonction du nombre de répétitions on peut utiliser la fonction [counter](https://docs.python.org/2/library/collections.html#collections.Counter), exemple: >>> from collections import Counter >>> l = [8, 3, 9, 2, 7, 1, 3, 2, 0, 0, 7, 8, 6, 9, 4, 6, 3, 3, 2, 5] >>>

How to find the element of a list with the maximum of repetitions (occurrences) in python ?

To find he element of a list with the maximum of repetitions (occurrences) in python, a solution is to use the function [counter](https://docs.python.org/2/library/collections.html#collections.Counter) from the python module collections, example: [TOC] ### Find the element of a list with the maximum of repetitions for a list of numbers Example with a list of numbers: >>> from collections import Counter >>> l = [8, 3, 9, 2, 7, 1, 3, 2, 0, 0, 7, 8, 6, 9, 4, 6, 3, 3, 2, 5]

How to convert a base64 image in png format using python ?

Example of how to convert a base64 image in png format using python (the input file base64.txt used in the following example can be found [here](https://www.science-emergence.com/Files/base64/)): import base64 from PIL import Image from io import BytesIO f = open('base64.txt', 'r') data = f.read() f.closed im = Image.open(BytesIO(base64.b64decode(data))) im.save('image.png', 'PNG') returns: [image:550 size:50 caption:How to save a base64 image in png format using pyt