Mis a jour le 2017-12-03, 22:17

Configuration des axes

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')
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 de la fonte (police) de graduation de l'axe des x :
for tickLabel in pyplot.gca().get_xaxis().get_ticklabels():
    tickLabel.set_fontsize(8)

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