|
| 1 | +""" |
| 2 | +Visualization of named colors. |
| 3 | +
|
| 4 | +Simple plot example with the named colors and its visual representation. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import (absolute_import, division, print_function, |
| 8 | + unicode_literals) |
| 9 | + |
| 10 | +import six |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +from matplotlib import colors |
| 15 | + |
| 16 | + |
| 17 | +colors_ = list(six.iteritems(colors.cnames)) |
| 18 | + |
| 19 | +# Add the single letter colors. |
| 20 | +for name, rgb in six.iteritems(colors.ColorConverter.colors): |
| 21 | + hex_ = colors.rgb2hex(rgb) |
| 22 | + colors_.append((name, hex_)) |
| 23 | + |
| 24 | +# Transform to hex color values. |
| 25 | +hex_ = [color[1] for color in colors_] |
| 26 | +# Get the rgb equivalent. |
| 27 | +rgb = [colors.hex2color(color) for color in hex_] |
| 28 | +# Get the hsv equivalent. |
| 29 | +hsv = [colors.rgb_to_hsv(color) for color in rgb] |
| 30 | + |
| 31 | +# Split the hsv values to sort. |
| 32 | +hue = [color[0] for color in hsv] |
| 33 | +sat = [color[1] for color in hsv] |
| 34 | +val = [color[2] for color in hsv] |
| 35 | + |
| 36 | +# Sort by hue, saturation and value. |
| 37 | +ind = np.lexsort((val, sat, hue)) |
| 38 | +sorted_colors = [colors_[i] for i in ind] |
| 39 | + |
| 40 | +n = len(sorted_colors) |
| 41 | +ncols = 3 |
| 42 | +nrows = int(np.ceil(1. * n / ncols)) |
| 43 | + |
| 44 | +fig = plt.figure(figsize=(ncols*5, nrows*4)) |
| 45 | +for i, (name, color) in enumerate(sorted_colors): |
| 46 | + ax = fig.add_subplot(nrows, ncols, i + 1) |
| 47 | + ax.text(0.55, 0.5, name, fontsize=12, |
| 48 | + horizontalalignment='left', |
| 49 | + verticalalignment='center') |
| 50 | + |
| 51 | + # Add extra black line a little bit thicker to make |
| 52 | + # clear colors more visible. |
| 53 | + ax.hlines(0.5, 0, 0.5, color='black', linewidth=10) |
| 54 | + ax.hlines(0.5, 0, 0.5, color=color, linewidth=8) |
| 55 | + ax.set_xlim(0, 1) |
| 56 | + ax.set_ylim(0, 1) |
| 57 | + ax.set_axis_off() |
| 58 | + |
| 59 | +fig.subplots_adjust(left=0.01, right=0.99, |
| 60 | + top=0.99, bottom=0.01, |
| 61 | + hspace=1, wspace=0.1) |
| 62 | +plt.show() |
0 commit comments