Mis a jour le 2024-03-17, 13:3

Histogrammes

Displot

displot : permet de faire des histogrammes et des courbes de densité.
seaborn.displot(v, color = 'green', kind = 'hist', rug = True) : trace l'histogramme des valeurs de v. Paramètres : import random random.seed(0) v = [random.gauss(0, 1) for i in range(300)] seaborn.displot(v, color = 'green', kind = 'hist', rug = True)
On peut tracer plusieurs histogrammes ou densités en fonction de certaines colonnes : seaborn.displot(x = 'v', data = df, color = 'green', hue = 'A', row = 'B', col = 'C', kind = 'kde', rug = True, palette = {'a': 'red', 'b': 'green', 'c': 'blue'})
Exemple complet : import random random.seed(0) df = pandas.DataFrame({'v': [random.gauss(0, 1) for i in range(60)], 'A': sum([['a', 'b', 'c'] for i in range(20)], []), 'B': sum([['b1', 'b2'] for i in range(30)], []), 'C': sum([['c1', 'c2', 'c2', 'c1'] for i in range(15)], [])}) df.loc[df['A'] == 'b', 'v'] += 0.2 df.loc[df['A'] == 'c', 'v'] += 0.3 df.loc[df['B'] == 'b2', 'v'] += 0.1 df.loc[df['C'] == 'c2', 'v'] -= 0.1 fg = seaborn.displot(x = 'v', data = df, color = 'green', hue = 'A', row = 'B', col = 'C', kind = 'kde', rug = True, palette = {'a': 'red', 'b': 'green', 'c': 'blue'}) fg.set_xlabels('abscisse') fg.set_ylabels('ordonnee') fg.tight_layout()

Histplot

histplot : analogue à displot, avec kind = 'hist' (histogramme) :

Ecdfplot

Ca trace la distribution cumulée (Empirical Cumulative Distribution Function)
seaborn.ecdfplot(x = 'val', data = df) : trace la distribution cumulée pour la variable val du dataframe. Paramètres :
Exemple : import random random.seed(0) ind = range(200) v = [random.gauss(0, 1) for i in ind] + [random.gauss(4, 1) for i in ind] df = pandas.DataFrame({'val': v, 'lab': ['a' for i in ind] + ['b' for i in ind]}) seaborn.ecdfplot(x = 'val', data = df, hue = 'lab', hue_order = ['b', 'a'], palette = {'a': 'red', 'b': 'blue'})

Kdeplot

On peut aussi tracer directement la fonction de densité d'une variable
Traçage d'une densité en 2 dimensions (avec 2 variables) : import random x = numpy.array([random.gauss(0, 1) for i in range(300)]) y = x + numpy.array([random.gauss(0, 0.2) for i in range(300)]) seaborn.kdeplot(x, y, fill = True, n_levels = 7, cbar = True)

Distributions 2d

Représentation d'une Distribution 2d : import random random.seed(0) df = pandas.DataFrame({'v1': [random.gauss(0, 1) for i in range(100)], 'A': sum([['a', 'b'] for i in range(50)], [])}) df['v2'] = [df['v1'].loc[i] * (1 if df.loc[i, 'A'] == 'a' else -1) + random.gauss(0, 0.2) for i in range(len(df))] seaborn.kdeplot(x = 'v1', y = 'v2', data = df, color = 'green', hue = 'A', levels = 8, bw_adjust = 1)
Traçage du nuage de points : import random x = numpy.array([random.gauss(0, 1) for i in range(300)]) y = x + numpy.array([random.gauss(0, 0.2) for i in range(300)]) seaborn.jointplot(x, y)
jointplot retourne un objet seaborn.axisgrid.JointGrid qui a des fonctions qui permettent de customiser le graphe ou de rajouter des éléments dessus.

Copyright python-simple.com
programmer en python, tutoriel python, graphes en python, Aymeric Duclert