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

Skip to content

Commit 7115159

Browse files
committed
adding axes one at a time
1 parent 20d21c0 commit 7115159

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

examples/color/named_colors.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,45 @@
22
import matplotlib.pyplot as plt
33
from matplotlib import cbook
44
from matplotlib import colors
5-
from itertools import chain
65

7-
n = len(colors.cnames)
8-
ncols = n / 15
9-
nrows = n / ncols
6+
colors_ = colors.cnames.items()
107

11-
names = colors.cnames.keys()
12-
hex_ = colors.cnames.values()
8+
#add the single letter colors
9+
for name, rgb in colors.ColorConverter.colors.items():
10+
hex_ = colors.rgb2hex(rgb)
11+
colors_.append((name, hex_))
12+
13+
#hex color values
14+
hex_ = [color[1] for color in colors_]
15+
#rgb equivalent
1316
rgb = [colors.hex2color(color) for color in hex_]
17+
#hsv equivalent
1418
hsv = [colors.rgb_to_hsv(color) for color in rgb]
1519

20+
#split the hsv to sort
1621
hue = [color[0] for color in hsv]
1722
sat = [color[1] for color in hsv]
1823
val = [color[2] for color in hsv]
1924

25+
#sort by hue, saturation and value
2026
ind = np.lexsort((val, sat, hue))
21-
22-
names_ = []
23-
colors_ = []
24-
25-
for i in ind:
26-
names_.append(names[i])
27-
colors_.append(hex_[i])
28-
29-
fig, axes_ = plt.subplots(nrows=nrows, ncols=ncols)
30-
axes = list(chain(*axes_))
31-
32-
for i in range(n):
33-
title = axes[i].set_title(names_[i])
27+
sorted_colors = [colors_[i] for i in ind]
28+
29+
n = len(sorted_colors)
30+
ncols = 10
31+
nrows = int(np.ceil(1. * n / ncols))
32+
33+
fig = plt.figure()
34+
for i, (name, color) in enumerate(sorted_colors):
35+
ax = fig.add_subplot(nrows, ncols, i + 1,
36+
axisbg=color,
37+
xticks=[], yticks=[])
38+
title = ax.set_title(name)
3439
title.set_size('xx-small')
35-
axes[i].set_axis_bgcolor(colors_[i])
36-
axes[i].spines['right'].set_visible(False)
37-
axes[i].spines['top'].set_visible(False)
38-
axes[i].spines['bottom'].set_visible(False)
39-
axes[i].spines['left'].set_visible(False)
40-
axes[i].set_xticks([])
41-
axes[i].set_yticks([])
40+
ax.spines['right'].set_visible(False)
41+
ax.spines['top'].set_visible(False)
42+
ax.spines['bottom'].set_visible(False)
43+
ax.spines['left'].set_visible(False)
4244

4345
plt.tight_layout()
4446
plt.show()

0 commit comments

Comments
 (0)