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

Configuration des axes

Pour récupérer l'axe des x :
On utilise ici la forme objet avec des objets Figure, Axes et Axis :
pyplot.axis :
Exemple : figure = pyplot.figure() axes = figure.add_subplot(111) axes.scatter(range(5), [x ** 2 for x in range(5)]) axes.set_xlim(0, 4) axes.set_xlabel('axe des x') axes.xaxis.set_ticks(range(5)) axes.xaxis.set_ticklabels(['x=0', 'x=1', 'x=2', 'x=3', 'x=4']) axes.xaxis.set_ticks_position('bottom') axes.xaxis.set_label_position('top') axes.yaxis.set_tick_params(direction = 'out', length = 20, width = 5, color = 'red', labelsize = 20, pad = 20, labelcolor = 'violet', right = True, left = True) axes = figure.add_axes([0.3, 0.5, 0.3, 0.3]) axes.patch.set_color('lightyellow') axes.plot(range(5), range(5)) axes.xaxis.set_ticks(range(5)) axes.xaxis.set_ticklabels(map(lambda x: 'a' + str(x), range(5))) axes.set_yticks([0, 2, 4]) axes.set_yticks([1, 3], minor = True) axes.yaxis.set_ticklabels(['1', '3'], minor = True) axes.yaxis.set_tick_params(which = 'major', length = 15, width = 3, color = 'green', labelsize = 20, labelcolor = 'violet') axes.yaxis.set_tick_params(which = 'minor', length = 10, width = 1, color = 'green', labelsize = 10, labelcolor = 'violet') axes.yaxis.grid(True, color = 'orange', linewidth = 1, linestyle = 'dashed') axes.yaxis.grid(True, which = 'minor', color = 'orange', linewidth = 1, linestyle = 'dotted')
Pour inverser le sens d'un axe, par exemple avoir l'axe des y qui pointe vers le bas : pyplot.gca().invert_yaxis()
Configuration de la visibilité des axes :
Exemple de changement de la visiblité des axes figure = pyplot.figure() axes = figure.add_subplot(111) axes.scatter(range(5), [x ** 2 for x in range(5)], s = 50) axes.set_frame_on(False) axes.yaxis.tick_left() axes.xaxis.set_visible(False) (xmin, xmax) = axes.xaxis.get_view_interval() (ymin, ymax) = axes.yaxis.get_view_interval() axes.add_artist(pyplot.Line2D((xmin, xmin), (ymin, ymax), color = 'magenta', linewidth = 3)) axes.add_artist(pyplot.Line2D((xmin, xmax), (ymin, ymin), color = 'cyan', linewidth = 5))
Marges dans les graduations :
Exemple avec la formulation objet : figure = pyplot.figure() axes = figure.add_subplot(2, 1, 1) axes.scatter(range(5), [x ** 2 for x in range(5)], s = 50, color = 'blue') axes.margins(1, 0.5) axes = figure.add_subplot(2, 1, 2) axes.scatter(range(5), [x ** 2 for x in range(5)], s = 50, color = 'red') axes.margins(0, 0)
Pour changer la taille du label : myText = pyplot.xlabel('my label); myText.set_fontsize(16)
Pour changer la taille de la fonte (police) de graduation de l'axe des x :
for tickLabel in pyplot.gca().xaxis.get_ticklabels():
    tickLabel.set_fontsize(8)
  
Pour faire tourner les étiquettes de 90 degrés : pyplot.setp(ax.get_xticklabels(), rotation = 90)
Pour changer le background de certains ticks :
for tickLabel in pyplot.gca().xaxis.get_ticklabels():
  if tickLabel.get_text() in ['A', 'B', C']:
    tickLabel.set_backgroundcolor('yellow')
  
Pour imposer d'avoir au maxmimm 8 ticks avec un label : pyplot.gca().xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(8))
pyplot.gca().set_aspect(aspect = 'equal') : même échelle sur les 2 axes.
Pour avoir sur la même figure 2 graphes avec 2 échelles différentes sur l'axe des y et partageant leur axe des x, utiliser twinx() : pl = pyplot.plot([1, 4, 3, 7, 8], color = 'blue') pyplot.legend(pl, ['blue']) ax2 = pyplot.gca().twinx() pl2 = ax2.plot([80, 70, 30, 40, 10], color = 'red') pyplot.legend(pl2, ['red']) utiliser twiny pour un axe des y commun.
Locators : permet d'indiquer où placer les graduations :

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