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

Ajout de légende

Par défaut, la construction de la légende est automatique :
Exemple : pyplot.scatter([0, 1, 2, 3, 4], [6, 4, 6, 4, 5], label = 'A', color = 'red') pyplot.scatter([0, 1, 2, 3, 4], [5, 3, 7, 5, 3], label = 'B', color = 'blue') pyplot.legend()
On peut aussi récupérer les handles des graphes et mettre son propre texte :
g1 = pyplot.scatter([0, 1, 2, 3, 4], [6, 4, 6, 4, 5], color = 'red')
2 = pyplot.scatter([0, 1, 2, 3, 4], [5, 3, 7, 5, 3], color = 'blue')
pyplot.legend([g1, g2], ['valeurs de A', 'valeurs de B'])
  
Attention, dans le cas de pyplot.plot, ce qu'on récupère est déjà une liste (au cas où il y a plusieurs séries, donc faire :
g1 = pyplot.plot([0, 1, 2, 3, 4], [6, 4, 6, 4, 5], color = 'red')
2 = pyplot.plot([0, 1, 2, 3, 4], [5, 3, 7, 5, 3], color = 'blue')
pyplot.legend(g1 + g2, ['valeurs de A', 'valeurs de B'])
  
Paramètres des légendes :
Exemple de code : g1 = pyplot.scatter([0, 1, 2, 3, 4], [6, 4, 6, 4, 5], label = 'A', color = 'red') g2 = pyplot.scatter([0, 1, 2, 3, 4], [5, 3, 7, 5, 3], label = 'B', color = 'blue') pyplot.legend(loc = 'upper left', fontsize = 8, title = 'mon titre', title_fontsize = 15, labelcolor = 'green', labelspacing = 1.5, borderpad = 1.5, frameon = True, shadow = True, fancybox = True, facecolor = 'lemonchiffon', edgecolor = 'green', markerscale = 2, bbox_to_anchor = (0.5, 1))
Pour enlever la légende : pyplot.gca().legend().set_visible(False)
Pour modifier une légende existante :
Pour modifier le texte d'une légende :
exemple : g1 = pyplot.scatter([0, 1, 2, 3, 4], [6, 4, 6, 4, 5], label = 'A', color = 'red') g2 = pyplot.scatter([0, 1, 2, 3, 4], [5, 3, 7, 5, 3], label = 'B', color = 'blue') myLegend = pyplot.legend() myLegend.texts[0].set(backgroundcolor = 'yellow', color = 'red', fontsize = 15, fontweight = 'bold', fontstyle = 'italic', rotation = 90, text = 'a', x = 10)

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