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

Boxplots, stripplot, swarmplot, violinplot, barplot

catplot est la fonction générique qui permet de faire des plots d'une variable quantitative en fonction d'une variable catégorie : des stripplot, des swarmplots, des boxplot, des violinplot, des boxenplot, des pointplot, barplot ou countplot :
Ce sont ici les représentations d'une variable continue en fonction de catégories. Les valeurs peuvent être fournies de différentes façons :

Stripplot

Stripplot : permet de représenter un nuage de points 1d pour chaque valeur de catégorie : df = pandas.DataFrame({'X': ['A', 'A', 'A', 'B', 'B', 'B', 'B'], 'Y': [2, 4, 5, 9, 16, 7, 12], 'Z': ['a', 'b', 'a', 'b', 'a', 'b', 'a']}) seaborn.stripplot(x = 'X', y = 'Y', hue = 'Z', order = ['B', 'A'], hue_order = ['b', 'a'], palette = {'a': 'red', 'b': 'blue'}, jitter = 0.2, size = 10, data = df)

Swarmplot

Swarmplot :
Si warning car trop grand nombre de points, on peut essayer de diminuer la taille des points : size = 2 (le défaut est 5).
Si on veut faire un graphe swarmplot en coordonnées log :

Boxplot

Boxplot : df = pandas.DataFrame({'X': ['A', 'A', 'A', 'B', 'B', 'B', 'B'], 'Y': [2, 4, 5, 9, 16, 7, 12], 'Z': ['a', 'b', 'a', 'b', 'a', 'b', 'a']}) seaborn.boxplot(x = 'X', y = 'Y', hue = 'Z', order = ['B', 'A'], hue_order = ['b', 'a'], palette = {'a': 'orange', 'b': 'yellow'}, saturation = 1, width = 0.5, linewidth = 0.5, data = df)
Configuration avancée en modifiant les couleurs :
On peut superposer un swarmplot au-dessus d'un boxplot : df = pandas.DataFrame({'X': ['A', 'B', 'A', 'A', 'B', 'A', 'A', 'B'], 'Y': [3, 7, 2, 4, 8, 4, 3.5, 7.2]}) seaborn.boxplot(x = 'X', y = 'Y', data = df, color = 'skyblue', fliersize = 0) seaborn.swarmplot(x = 'X', y = 'Y', data = df, color = 'blue')

Violinplot

Violinplot :
Modifier les couleurs d'un violinplot :
seaborn.violinplot(x = 'myCateg', y = 'myValue', data = df, color = 'white')
for art in pyplot.gca().get_children():
    if type(art) in [matplotlib.collections.PolyCollection, matplotlib.collections.PathCollection]:
        art.set(edgecolor = 'black')
    elif type(art) == matplotlib.lines.Line2D:
        art.set(color = 'black')
  

Boxenplot

Evolution du boxplot, avec plus de quantiles.
Exemple : df = pandas.DataFrame({'X': ['A', 'A', 'A', 'B', 'B', 'B', 'B'], 'Y': [2, 4, 5, 9, 16, 7, 12], 'Z': ['a', 'b', 'a', 'b', 'a', 'b', 'a']}) seaborn.boxenplot(x = 'X', y = 'Y', hue = 'Z', color = 'red', data = df)
boxenplot :

Barplot

Barplot :
Barres d'erreurs sur les barplots : df = pandas.DataFrame({'X': ['A', 'A', 'A', 'B', 'B', 'B'], 'Y': [2, 4, 6, 12, 15, 18]}) seaborn.barplot(x = 'X', y = 'Y', color = 'yellow', ci = 'sd', errcolor = 'red', errwidth = 0.5, capsize = 0.2, data = df)

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