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

Skip to content

Commit 29d8e1e

Browse files
committed
docstring and pep8
1 parent d38932a commit 29d8e1e

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

examples/color/named_colors.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1+
"""
2+
Visualization of named colors.
3+
4+
Simple plot example with the named colors and its visual representation.
5+
"""
6+
17
import numpy as np
28
import matplotlib.pyplot as plt
3-
from matplotlib import cbook
4-
from matplotlib import colors
5-
9+
from matplotlib import colors
10+
611
colors_ = colors.cnames.items()
7-
8-
#add the single letter colors
12+
13+
# Add the single letter colors.
914
for name, rgb in colors.ColorConverter.colors.items():
1015
hex_ = colors.rgb2hex(rgb)
1116
colors_.append((name, hex_))
12-
13-
#hex color values
17+
18+
# Transform to hex color values.
1419
hex_ = [color[1] for color in colors_]
15-
#rgb equivalent
20+
# Get the rgb equivalent.
1621
rgb = [colors.hex2color(color) for color in hex_]
17-
#hsv equivalent
22+
# Get the hsv equivalent.
1823
hsv = [colors.rgb_to_hsv(color) for color in rgb]
19-
20-
#split the hsv to sort
24+
25+
# Split the hsv values to sort.
2126
hue = [color[0] for color in hsv]
2227
sat = [color[1] for color in hsv]
2328
val = [color[2] for color in hsv]
24-
25-
#sort by hue, saturation and value
29+
30+
# Sort by hue, saturation and value.
2631
ind = np.lexsort((val, sat, hue))
2732
sorted_colors = [colors_[i] for i in ind]
28-
33+
2934
n = len(sorted_colors)
3035
ncols = 3
3136
nrows = int(np.ceil(1. * n / ncols))
32-
37+
3338
fig = plt.figure(figsize=(ncols*5, nrows*4))
3439
for i, (name, color) in enumerate(sorted_colors):
35-
ax = fig.add_subplot(nrows, ncols, i + 1,
36-
xticks=[], yticks=[])
40+
ax = fig.add_subplot(nrows, ncols, i + 1)
3741
ax.text(0.55, 0.5, name, fontsize=12,
38-
horizontalalignment='left',
39-
verticalalignment='center')
40-
41-
ax.hlines(0.5, 0, 0.5, color='black', linewidth=9)
42+
horizontalalignment='left',
43+
verticalalignment='center')
44+
45+
# Add extra black line a little bit thicker to make
46+
# clear colors more visible.
47+
ax.hlines(0.5, 0, 0.5, color='black', linewidth=10)
4248
ax.hlines(0.5, 0, 0.5, color=color, linewidth=8)
4349
ax.set_xlim(0, 1)
4450
ax.set_ylim(0, 1)

0 commit comments

Comments
 (0)