From 3a596d35e5688ce70e6279d81f6aa8d0ef8f3c13 Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Thu, 6 Feb 2014 17:13:17 -0500 Subject: [PATCH 1/6] colors unsorted --- examples/color/named_colors.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/color/named_colors.py diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py new file mode 100644 index 000000000000..ff38fec21abd --- /dev/null +++ b/examples/color/named_colors.py @@ -0,0 +1,35 @@ +#issue https://github.com/matplotlib/matplotlib/issues/2164 + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.colors import cnames +from itertools import chain + +print len(cnames.keys()) +n = len(cnames) +ncols = n / 15 +nrows = n / ncols +names = cnames.keys() +colors = cnames.values() + +fig, axes_ = plt.subplots(nrows=nrows, ncols=ncols) +axes = list(chain(*axes_)) + +for i in range(n): + title = axes[i].set_title(names[i]) + title.set_size('xx-small') + axes[i].set_axis_bgcolor(colors[i]) + axes[i].spines['right'].set_visible(False) + axes[i].spines['top'].set_visible(False) + axes[i].spines['bottom'].set_visible(False) + axes[i].spines['left'].set_visible(False) + axes[i].set_xticks([]) + axes[i].set_yticks([]) + +# axes[i].set_label(names[i]) + +# for axes in list(chain(*axes_)): +# axes.set_axis_off() + +# print nrows, ncols, fig, axes +plt.show() \ No newline at end of file From 20d21c00037906e3dc43020ce73f122c6111bb49 Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Mon, 10 Feb 2014 13:33:10 -0500 Subject: [PATCH 2/6] sorted by hue, sat, val --- examples/color/named_colors.py | 41 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index ff38fec21abd..270f74ad948e 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -1,24 +1,38 @@ -#issue https://github.com/matplotlib/matplotlib/issues/2164 - import numpy as np import matplotlib.pyplot as plt -from matplotlib.colors import cnames +from matplotlib import cbook +from matplotlib import colors from itertools import chain -print len(cnames.keys()) -n = len(cnames) +n = len(colors.cnames) ncols = n / 15 nrows = n / ncols -names = cnames.keys() -colors = cnames.values() + +names = colors.cnames.keys() +hex_ = colors.cnames.values() +rgb = [colors.hex2color(color) for color in hex_] +hsv = [colors.rgb_to_hsv(color) for color in rgb] + +hue = [color[0] for color in hsv] +sat = [color[1] for color in hsv] +val = [color[2] for color in hsv] + +ind = np.lexsort((val, sat, hue)) + +names_ = [] +colors_ = [] + +for i in ind: + names_.append(names[i]) + colors_.append(hex_[i]) fig, axes_ = plt.subplots(nrows=nrows, ncols=ncols) axes = list(chain(*axes_)) for i in range(n): - title = axes[i].set_title(names[i]) + title = axes[i].set_title(names_[i]) title.set_size('xx-small') - axes[i].set_axis_bgcolor(colors[i]) + axes[i].set_axis_bgcolor(colors_[i]) axes[i].spines['right'].set_visible(False) axes[i].spines['top'].set_visible(False) axes[i].spines['bottom'].set_visible(False) @@ -26,10 +40,5 @@ axes[i].set_xticks([]) axes[i].set_yticks([]) -# axes[i].set_label(names[i]) - -# for axes in list(chain(*axes_)): -# axes.set_axis_off() - -# print nrows, ncols, fig, axes -plt.show() \ No newline at end of file +plt.tight_layout() +plt.show() From 7115159bfd975f0ae8ba9c1e7a50fe5aa370bc46 Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Mon, 10 Feb 2014 14:53:39 -0500 Subject: [PATCH 3/6] adding axes one at a time --- examples/color/named_colors.py | 54 ++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index 270f74ad948e..a7e9756c22f0 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -2,43 +2,45 @@ import matplotlib.pyplot as plt from matplotlib import cbook from matplotlib import colors -from itertools import chain -n = len(colors.cnames) -ncols = n / 15 -nrows = n / ncols +colors_ = colors.cnames.items() -names = colors.cnames.keys() -hex_ = colors.cnames.values() +#add the single letter colors +for name, rgb in colors.ColorConverter.colors.items(): + hex_ = colors.rgb2hex(rgb) + colors_.append((name, hex_)) + +#hex color values +hex_ = [color[1] for color in colors_] +#rgb equivalent rgb = [colors.hex2color(color) for color in hex_] +#hsv equivalent hsv = [colors.rgb_to_hsv(color) for color in rgb] +#split the hsv to sort hue = [color[0] for color in hsv] sat = [color[1] for color in hsv] val = [color[2] for color in hsv] +#sort by hue, saturation and value ind = np.lexsort((val, sat, hue)) - -names_ = [] -colors_ = [] - -for i in ind: - names_.append(names[i]) - colors_.append(hex_[i]) - -fig, axes_ = plt.subplots(nrows=nrows, ncols=ncols) -axes = list(chain(*axes_)) - -for i in range(n): - title = axes[i].set_title(names_[i]) +sorted_colors = [colors_[i] for i in ind] + +n = len(sorted_colors) +ncols = 10 +nrows = int(np.ceil(1. * n / ncols)) + +fig = plt.figure() +for i, (name, color) in enumerate(sorted_colors): + ax = fig.add_subplot(nrows, ncols, i + 1, + axisbg=color, + xticks=[], yticks=[]) + title = ax.set_title(name) title.set_size('xx-small') - axes[i].set_axis_bgcolor(colors_[i]) - axes[i].spines['right'].set_visible(False) - axes[i].spines['top'].set_visible(False) - axes[i].spines['bottom'].set_visible(False) - axes[i].spines['left'].set_visible(False) - axes[i].set_xticks([]) - axes[i].set_yticks([]) + ax.spines['right'].set_visible(False) + ax.spines['top'].set_visible(False) + ax.spines['bottom'].set_visible(False) + ax.spines['left'].set_visible(False) plt.tight_layout() plt.show() From d38932a447191136239b1419223dad8d04d1fa86 Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Tue, 11 Feb 2014 09:41:09 -0500 Subject: [PATCH 4/6] three column arrangement --- examples/color/named_colors.py | 40 +++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index a7e9756c22f0..16b14dc72f10 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -2,45 +2,49 @@ import matplotlib.pyplot as plt from matplotlib import cbook from matplotlib import colors - + colors_ = colors.cnames.items() - + #add the single letter colors for name, rgb in colors.ColorConverter.colors.items(): hex_ = colors.rgb2hex(rgb) colors_.append((name, hex_)) - + #hex color values hex_ = [color[1] for color in colors_] #rgb equivalent rgb = [colors.hex2color(color) for color in hex_] #hsv equivalent hsv = [colors.rgb_to_hsv(color) for color in rgb] - + #split the hsv to sort hue = [color[0] for color in hsv] sat = [color[1] for color in hsv] val = [color[2] for color in hsv] - + #sort by hue, saturation and value ind = np.lexsort((val, sat, hue)) sorted_colors = [colors_[i] for i in ind] - + n = len(sorted_colors) -ncols = 10 +ncols = 3 nrows = int(np.ceil(1. * n / ncols)) - -fig = plt.figure() + +fig = plt.figure(figsize=(ncols*5, nrows*4)) for i, (name, color) in enumerate(sorted_colors): ax = fig.add_subplot(nrows, ncols, i + 1, - axisbg=color, xticks=[], yticks=[]) - title = ax.set_title(name) - title.set_size('xx-small') - ax.spines['right'].set_visible(False) - ax.spines['top'].set_visible(False) - ax.spines['bottom'].set_visible(False) - ax.spines['left'].set_visible(False) - -plt.tight_layout() + ax.text(0.55, 0.5, name, fontsize=12, + horizontalalignment='left', + verticalalignment='center') + + ax.hlines(0.5, 0, 0.5, color='black', linewidth=9) + ax.hlines(0.5, 0, 0.5, color=color, linewidth=8) + ax.set_xlim(0, 1) + ax.set_ylim(0, 1) + ax.set_axis_off() + +fig.subplots_adjust(left=0.01, right=0.99, + top=0.99, bottom=0.01, + hspace=1, wspace=0.1) plt.show() From 29d8e1eff2a740c5ea03f4d35ba42ed9fb213b3d Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Wed, 12 Feb 2014 10:40:18 -0500 Subject: [PATCH 5/6] docstring and pep8 --- examples/color/named_colors.py | 48 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index 16b14dc72f10..fb39d6a492eb 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -1,44 +1,50 @@ +""" +Visualization of named colors. + +Simple plot example with the named colors and its visual representation. +""" + import numpy as np import matplotlib.pyplot as plt -from matplotlib import cbook -from matplotlib import colors - +from matplotlib import colors + colors_ = colors.cnames.items() - -#add the single letter colors + +# Add the single letter colors. for name, rgb in colors.ColorConverter.colors.items(): hex_ = colors.rgb2hex(rgb) colors_.append((name, hex_)) - -#hex color values + +# Transform to hex color values. hex_ = [color[1] for color in colors_] -#rgb equivalent +# Get the rgb equivalent. rgb = [colors.hex2color(color) for color in hex_] -#hsv equivalent +# Get the hsv equivalent. hsv = [colors.rgb_to_hsv(color) for color in rgb] - -#split the hsv to sort + +# Split the hsv values to sort. hue = [color[0] for color in hsv] sat = [color[1] for color in hsv] val = [color[2] for color in hsv] - -#sort by hue, saturation and value + +# Sort by hue, saturation and value. ind = np.lexsort((val, sat, hue)) sorted_colors = [colors_[i] for i in ind] - + n = len(sorted_colors) ncols = 3 nrows = int(np.ceil(1. * n / ncols)) - + fig = plt.figure(figsize=(ncols*5, nrows*4)) for i, (name, color) in enumerate(sorted_colors): - ax = fig.add_subplot(nrows, ncols, i + 1, - xticks=[], yticks=[]) + ax = fig.add_subplot(nrows, ncols, i + 1) ax.text(0.55, 0.5, name, fontsize=12, - horizontalalignment='left', - verticalalignment='center') - - ax.hlines(0.5, 0, 0.5, color='black', linewidth=9) + horizontalalignment='left', + verticalalignment='center') + + # Add extra black line a little bit thicker to make + # clear colors more visible. + ax.hlines(0.5, 0, 0.5, color='black', linewidth=10) ax.hlines(0.5, 0, 0.5, color=color, linewidth=8) ax.set_xlim(0, 1) ax.set_ylim(0, 1) From e29262faec517f31adc06f0831c6c91d707701cc Mon Sep 17 00:00:00 2001 From: Federico Ariza Date: Wed, 12 Feb 2014 12:12:11 -0500 Subject: [PATCH 6/6] six and future imports --- examples/color/named_colors.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index fb39d6a492eb..8567c0907864 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -4,14 +4,20 @@ Simple plot example with the named colors and its visual representation. """ +from __future__ import (absolute_import, division, print_function, + unicode_literals) + +import six + import numpy as np import matplotlib.pyplot as plt from matplotlib import colors -colors_ = colors.cnames.items() + +colors_ = list(six.iteritems(colors.cnames)) # Add the single letter colors. -for name, rgb in colors.ColorConverter.colors.items(): +for name, rgb in six.iteritems(colors.ColorConverter.colors): hex_ = colors.rgb2hex(rgb) colors_.append((name, hex_))