Mis a jour le 2024-10-20, 20:14

Barplot

Barplot :
Exemple complet de barplot : pyplot.bar(range(5), [1, 3, 3, 5, 4], width = 0.6, color = 'yellow', edgecolor = 'blue', linewidth = 2, yerr = [0.5, 1, 2, 1, 2], ecolor = 'magenta', capsize = 10) pyplot.xticks(range(5), ['A', 'B', 'C', 'D', 'E'], rotation = 45)
Exemple de barplot avec 2 séries de données : barWidth = 0.4 y1 = [1, 2, 4, 3] y2 = [3, 4, 4, 3] r1 = range(len(y1)) r2 = [x + barWidth for x in r1] pyplot.bar(r1, y1, width = barWidth, color = ['yellow' for i in y1], edgecolor = ['blue' for i in y1], linewidth = 2) pyplot.bar(r2, y2, width = barWidth, color = ['pink' for i in y1], edgecolor = ['green' for i in y1], linewidth = 4) pyplot.xticks([r + barWidth / 2 for r in range(len(y1))], ['A', 'B', 'C', 'D'])
Exemple de barplot avec 2 séries superposées : barWidth = 0.8 y1 = [1, 2, 4, 3] y2 = [3, 4, 4, 3] r = range(len(y1)) pyplot.bar(r, y1, width = barWidth, color = ['yellow' for i in y1], edgecolor = ['blue' for i in y1], linestyle = 'solid', hatch ='/', linewidth = 3) pyplot.bar(r, y2, width = barWidth, bottom = y1, color = ['pink' for i in y1], edgecolor = ['green' for i in y1], linestyle = 'dotted', hatch = 'o', linewidth = 3) pyplot.xticks(range(len(y1)), ['A', 'B', 'C', 'D'])
Barplot horizontal : barWidth = 0.8 y1 = [1, 2, 4, 3] y2 = [3, 4, 4, 3] r = range(len(y1)) pyplot.barh(r, y1, height = barWidth, color = ['yellow' for i in y1], edgecolor = ['blue' for i in y1], linestyle = 'solid', hatch ='/', linewidth = 3) pyplot.barh(r, y2, height = barWidth, left = y1, color = ['pink' for i in y1], edgecolor = ['green' for i in y1], linestyle = 'dotted', hatch = 'o', linewidth = 3) pyplot.yticks(range(len(y1)), ['A', 'B', 'C', 'D'])
Dans un barplot, pour supprimer les ticks sur l'axe des x (tout en conservant les labels) : faire pyplot.gca().axes.xaxis.set_ticks_position('none')
Pour customiser les labels :
for tickLabel in pyplot.gca().xaxis.get_ticklabels():
  if tickLabel.get_text() in ['A', 'C']:
    tickLabel.set_color('red')
  
tickLabel est de type matplotlib.text.Text
Lignes verticales : pyplot.vlines([0, 1, 3, 4], [0, 0, 1, 1], [5, 4, 3, 2], color = 'red', linestyle = 'solid', label = ['a', 'b', 'c', 'd']) df = pandas.DataFrame({'loc': [0.5, 1.5, 3.5, 4.5], 'min': [0, 0, 1, 1], 'max': [5, 4, 3, 2]}) pyplot.vlines(x = 'loc', ymin = 'min', ymax = 'max', data = df, color = 'blue', linestyle = 'solid', label = ['a', 'b', 'c', 'd'])
Barplot à partir d'une matrice : df = pandas.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [6, 4, 2, 1]}, index = ['a', 'b', 'c', 'd']) df.plot(kind = 'bar', xlabel = 'x values', ylabel = 'yvalues', color = {'A': 'red', 'B': 'green', 'C': 'blue'})
Stacked barplot d'un barplot avec des pourcentages dont le total par ligne fait 100% : df = pandas.DataFrame({'A': [10, 20, 10], 'B': [70, 50, 20], 'C': [20, 30, 70]}, index = ['a', 'b', 'c']) df.plot(kind = 'bar', stacked = True, width = 0.8, color = {'A': 'green', 'B': 'yellow', 'C': 'blue'})

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