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

Skip to content

Commit 5f25b5e

Browse files
committed
Merge pull request matplotlib#3506 from fariza/named-colors-figure-size
DOC : Improved named colors example
2 parents 26710ad + 3dbda0b commit 5f25b5e

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

examples/color/named_colors.py

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,42 @@
3838
sorted_colors = [colors_[i] for i in ind]
3939

4040
n = len(sorted_colors)
41-
ncols = 3
41+
ncols = 4
4242
nrows = int(np.ceil(1. * n / ncols))
4343

44-
fig = plt.figure(figsize=(ncols*2.5, nrows*2))
44+
fig = plt.figure()
45+
ax = fig.add_subplot(111)
46+
47+
X, Y = fig.get_dpi() * fig.get_size_inches()
48+
49+
# row height
50+
h = Y / (nrows + 1)
51+
# col width
52+
w = X / ncols
53+
4554
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,
55+
col = i % ncols
56+
row = int(i / ncols)
57+
y = Y - (row * h) - h
58+
59+
xi_line = w * (col + 0.05)
60+
xf_line = w * (col + 0.25)
61+
xi_text = w * (col + 0.3)
62+
63+
ax.text(xi_text, y, name, fontsize=(h * 0.8),
4864
horizontalalignment='left',
4965
verticalalignment='center')
5066

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)
67+
# Add extra black line a little bit thicker to make
68+
# clear colors more visible.
69+
ax.hlines(y, xi_line, xf_line, color='black', linewidth=(h * 0.7))
70+
ax.hlines(y + h * 0.1, xi_line, xf_line, color=color, linewidth=(h * 0.6))
71+
72+
ax.set_xlim(0, X)
73+
ax.set_ylim(0, Y)
74+
ax.set_axis_off()
75+
76+
fig.subplots_adjust(left=0, right=1,
77+
top=1, bottom=0,
78+
hspace=0, wspace=0)
6279
plt.show()

0 commit comments

Comments
 (0)