Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75b0d13

Browse files
committed
Update named_colors example.
1 parent f6f908e commit 75b0d13

1 file changed

Lines changed: 13 additions & 28 deletions

File tree

examples/color/named_colors.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,19 @@
1111

1212
import numpy as np
1313
import matplotlib.pyplot as plt
14-
from matplotlib import colors
14+
from matplotlib import colors as mcolors
1515

1616

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-
# Get the color names by themselves.
37-
names = [color[0] for color in colors_]
17+
colors = mcolors.CSS4_COLORS
3818

3919
# Sort by hue, saturation, value and name.
40-
ind = np.lexsort((names, val, sat, hue))
41-
sorted_colors = [colors_[i] for i in ind]
20+
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
21+
for name, color in colors.items())
22+
23+
# Get the sorted color names.
24+
sorted_names = [name for hsv, name in by_hsv]
4225

43-
n = len(sorted_colors)
26+
n = len(sorted_names)
4427
ncols = 4
4528
nrows = int(np.ceil(1. * n / ncols))
4629

@@ -53,7 +36,7 @@
5336
# col width
5437
w = X / ncols
5538

56-
for i, (name, color) in enumerate(sorted_colors):
39+
for i, name in enumerate(sorted_names):
5740
col = i % ncols
5841
row = int(i / ncols)
5942
y = Y - (row * h) - h
@@ -68,8 +51,10 @@
6851

6952
# Add extra black line a little bit thicker to make
7053
# clear colors more visible.
71-
ax.hlines(y, xi_line, xf_line, color='black', linewidth=(h * 0.7))
72-
ax.hlines(y + h * 0.1, xi_line, xf_line, color=color, linewidth=(h * 0.6))
54+
ax.hlines(
55+
y, xi_line, xf_line, color='black', linewidth=(h * 0.7))
56+
ax.hlines(
57+
y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6))
7358

7459
ax.set_xlim(0, X)
7560
ax.set_ylim(0, Y)

0 commit comments

Comments
 (0)