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

Skip to content

Commit 14b2c3a

Browse files
authored
Merge pull request #19188 from timhoffm/doc-colors
Cleanup comparision between X11/CSS4 and xkcd colors
2 parents c26b1a7 + c3f7729 commit 14b2c3a

1 file changed

Lines changed: 33 additions & 29 deletions

File tree

tutorials/colors/colors.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -142,52 +142,56 @@ def demo(sty):
142142
#
143143
# .. _xkcd-colors:
144144
#
145-
# xkcd v X11/CSS4
146-
# ---------------
145+
# Comparison between X11/CSS4 and xkcd colors
146+
# -------------------------------------------
147147
#
148-
# The xkcd colors come from a user survey conducted by the webcomic xkcd.
149-
# Details of the survey are available on the `xkcd blog
148+
# The xkcd colors come from a `user survey conducted by the webcomic xkcd
150149
# <https://blog.xkcd.com/2010/05/03/color-survey-results/>`__.
151150
#
152-
# There are 95 out of 148 colors with name collisions between the X11/CSS4
153-
# names and the xkcd names. Only three of these colors have the same hex
154-
# values.
151+
# 95 out of the 148 X11/CSS4 color names also appear in the xkcd color survey.
152+
# Almost all of them map to different color values in the X11/CSS4 and in
153+
# the xkcd palette. Only 'black', 'white' and 'cyan' are identical.
155154
#
156155
# For example, ``'blue'`` maps to ``'#0000FF'`` whereas ``'xkcd:blue'`` maps to
157156
# ``'#0343DF'``. Due to these name collisions, all xkcd colors have the
158157
# ``'xkcd:'`` prefix.
159158
#
160-
# The visual below shows name collisions. Color names where hex values agree
161-
# are bold.
159+
# The visual below shows name collisions. Color names where color values agree
160+
# are in bold.
162161

163162
import matplotlib._color_data as mcd
164163
import matplotlib.patches as mpatch
165164

166165
overlap = {name for name in mcd.CSS4_COLORS
167166
if "xkcd:" + name in mcd.XKCD_COLORS}
168167

169-
fig = plt.figure(figsize=[4.8, 16])
168+
fig = plt.figure(figsize=[9, 5])
170169
ax = fig.add_axes([0, 0, 1, 1])
171170

172-
for j, n in enumerate(sorted(overlap, reverse=True)):
173-
weight = None
174-
cn = mcd.CSS4_COLORS[n]
175-
xkcd = mcd.XKCD_COLORS["xkcd:" + n].upper()
176-
if cn == xkcd:
177-
weight = 'bold'
178-
179-
r1 = mpatch.Rectangle((0, j), 1, 1, color=cn)
180-
r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd)
181-
txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10,
182-
weight=weight)
183-
ax.add_patch(r1)
184-
ax.add_patch(r2)
185-
ax.axhline(j, color='k')
186-
187-
ax.text(.5, j + 1.5, 'X11', ha='center', va='center')
188-
ax.text(1.5, j + 1.5, 'xkcd', ha='center', va='center')
189-
ax.set_xlim(0, 3)
190-
ax.set_ylim(0, j + 2)
171+
n_groups = 3
172+
n_rows = len(overlap) // n_groups + 1
173+
174+
for j, color_name in enumerate(sorted(overlap)):
175+
css4 = mcd.CSS4_COLORS[color_name]
176+
xkcd = mcd.XKCD_COLORS["xkcd:" + color_name].upper()
177+
178+
col_shift = (j // n_rows) * 3
179+
y_pos = j % n_rows
180+
text_args = dict(va='center', fontsize=10,
181+
weight='bold' if css4 == xkcd else None)
182+
ax.add_patch(mpatch.Rectangle((0 + col_shift, y_pos), 1, 1, color=css4))
183+
ax.add_patch(mpatch.Rectangle((1 + col_shift, y_pos), 1, 1, color=xkcd))
184+
ax.text(0 + col_shift, y_pos + .5, ' ' + css4, alpha=0.5, **text_args)
185+
ax.text(1 + col_shift, y_pos + .5, ' ' + xkcd, alpha=0.5, **text_args)
186+
ax.text(2 + col_shift, y_pos + .5, ' ' + color_name, **text_args)
187+
188+
for g in range(n_groups):
189+
ax.hlines(range(n_rows), 3*g, 3*g + 2.8, color='0.7', linewidth=1)
190+
ax.text(0.5 + 3*g, -0.5, 'X11', ha='center', va='center')
191+
ax.text(1.5 + 3*g, -0.5, 'xkcd', ha='center', va='center')
192+
193+
ax.set_xlim(0, 3 * n_groups)
194+
ax.set_ylim(n_rows, -1)
191195
ax.axis('off')
192196

193197
plt.show()

0 commit comments

Comments
 (0)