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

Couleurs sous matplotlib

Couleurs

Les couleurs peuvent être indiquées de différentes façons :
Exemple de graphe avec différentes couleurs exprimées sous différentes formes : pyplot.figure() pyplot.scatter(range(5), [x ** 2 for x in range(5)], s = 100, color = ['blue', 'red', '0.70', 'r', '#ffee00']) pyplot.title('differentes couleurs sous differentes formes')
Pour avoir la liste des noms de couleurs disponibles : matplotlib.colors.cnames renvoie un dictionnaire nom de couleur vers couleur HTML : {'indigo': '#4B0082', 'gold': '#FFD700', ...}.
Couleurs disponibles sous forme textuelle : import matplotlib import colorsys columnNbr = 5 colorNames = list(matplotlib.colors.cnames.keys()) colorNames.sort(key = lambda x: colorsys.rgb_to_hsv( *matplotlib.colors.colorConverter.to_rgb(matplotlib.colors.cnames[x]))) colorNbr = len(colorNames) rowNbr = (colorNbr - 1) // columnNbr + 1 width = 1.0 / columnNbr height = 1.0 / rowNbr figure = pyplot.figure(figsize = (10, 10)) pyplot.gcf().subplots_adjust(0, 0, 1, 1) axes = figure.add_subplot(111) axes.set_frame_on(False) axes.xaxis.set_visible(False) axes.yaxis.set_visible(False) for i in range(rowNbr): y = 1 - (i + 1) * height for j in range(columnNbr): ind = j + columnNbr * i if ind >= len(colorNames): break x = j * width axes.add_artist(matplotlib.patches.Rectangle((x, y), width, height, color = colorNames[ind])) (r, g, b) = matplotlib.colors.colorConverter.to_rgb(colorNames[ind]) Y = 0.2126 * r + 0.7152 * g + 0.0722 * b if Y > 0.5: textColor = 'black' else: textColor = 'white' axes.text(x + width / 30, y + height / 2, colorNames[ind], color = textColor)
matplotlib.colors.colorConverter : objet singleton qui permet de convertir une couleur de n'importe quel format en triplet RGB :

Gammes de couleurs (color maps)

Utilisation d'une color map pour avoir des couleurs pour un nombre entre 0 et 1 :
Différentes types de color maps :
cmaps = [('sequentielles', ['viridis', 'plasma', 'inferno', 'magma', 'cividis', 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn', 'binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper']), ('Divergentes', ['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']), ('Cycliques', ['hsv', 'twilight', 'twilight_shifted']), ('Qualitatives', ['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']), ('Diverses', ['ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])] gradient = numpy.linspace(0, 1, 256) gradient = numpy.vstack((gradient, gradient)) nrows = sum([len(x[1]) for x in cmaps]) pyplot.figure(figsize = (6, nrows * 0.3)) n = 0 for categ, cms in cmaps: for cm in cms: n += 1 pyplot.subplot(nrows, 1, n) pyplot.imshow(gradient, aspect = 'auto', cmap = cm) pyplot.text(260, 0.8, cm, fontsize = 10) pyplot.gcf().subplots_adjust(left = 0, bottom = 0, right = 0.8, top = 1) pyplot.gca().set_axis_off()
Color map custom :
Faire une color map custom qui est grise quand la valeur n'est pas définie :
colorMap = matplotlib.cm.get_cmap('Blues').copy()
colorMap.set_bad('gray')
  

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