# Construire une fonction de répartition et la tracer avec python

Pour créer et tracer une fonction de repartition avec python, on peut utiliser la fonction numpy cumsum, exemple:

````from random import gauss`

`import matplotlib.pyplot as plt`
`import numpy as np`

`N = 10000`

`x = np.random.normal(size = N)`

`#----------------------------------------------------------------------------------------#`

`hist, bin_edges = np.histogram(x, bins=50, normed=True)`

`plt.bar(bin_edges[:-1], hist, width=bin_edges[1]-bin_edges[0], color='red', alpha=0.5)`

`plt.grid()`
`plt.title('Normal distribution \n with python')`
`plt.xlim(-4,4)`

`plt.savefig('cumulative_distribution_01.png', bbox_inches='tight')`
`plt.show()`
`plt.close()`

`#----------------------------------------------------------------------------------------#`
`# method 1 with numpy cumsum`

`dx = bin_edges[1] - bin_edges[0]`

`cumulative = np.cumsum(hist)*dx`

`plt.plot(bin_edges[:-1], cumulative, c='blue')`

`plt.grid()`
`plt.title('Cumulative distribution \n with numpy cumsum')`
`plt.xlim(-4,4)`
`plt.ylim(0,1)`

`plt.savefig('cumulative_distribution_cumsum.png', bbox_inches='tight')`
`plt.show()`
`plt.close()`
```

On peut utiliser aussi la fonction numpy sort:

````#----------------------------------------------------------------------------------------#`
`# method 2 with sort`

`X2 = np.sort(x)`
`F2 = np.array(range(N))/float(N)`

`plt.plot(X2, F2)`

`plt.grid()`
`plt.title('Cumulative distribution \n with numpy sort')`
`plt.xlim(-4,4)`
`plt.ylim(0,1)`

`plt.savefig('cumulative_distribution_sort.png', bbox_inches='tight')`
`plt.show()`
`plt.close()`
```

Image

of