10000 Merge pull request #147 from QuLogic/simplify-colormap · matplotlib/cheatsheets@e8f23b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit e8f23b6

Browse files
authored
Merge pull request #147 from QuLogic/simplify-colormap
Simplify colormap plotting
2 parents 3fece66 + a5c2769 commit e8f23b6

File tree

4 files changed

+20
-35
lines changed

4 files changed

+20
-35
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ check:
5050
./check-num-pages.sh handout-tips.pdf 1
5151
./check-num-pages.sh handout-beginner.pdf 1
5252
./check-num-pages.sh handout-intermediate.pdf 1
53-
./check-links.py cheatsheets.pdf
5453
./check-diffs.py
54+
./check-links.py cheatsheets.pdf
5555

5656
.PHONY: docs
5757
docs:

check-links.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
refs = [ref for ref in pdf.get_references() if ref.reftype == 'url']
1010

11-
status_codes = list(map(lambda ref: pdfx.downloader.get_status_code(ref.ref), refs))
11+
status_codes = [pdfx.downloader.get_status_code(ref.ref) for ref in refs]
1212

13-
broken_links = [refs[idx].ref for idx in range(len(refs)) if status_codes[idx] != 200]
13+
broken_links = [(ref.ref, code) for ref, code in zip(refs, status_codes) if code != 200]
1414

1515
# it seems that Twitter does not respond well to the link checker and throws a 400
16-
if all(['twitter.com' in url for url in broken_links]):
16+
if all(['twitter.com' in url for url, _ in broken_links]):
1717
sys.exit(0)
1818
else:
1919
print('Broken links:', broken_links)

scripts/colormaps.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,14 @@
3535
'terrain', 'ocean', 'gist_earth', 'cubehelix', 'rainbow'
3636
)
3737

38-
for cmap in cmaps:
39-
n = 512
38+
for name in cmaps:
39+
# The maximum number of segments in a cmap is 256, and for anything smaller,
40+
# the cmap will map as a suitably discrete set of colours.
41+
Z = np.linspace(0, 1, 256).reshape(1, 256)
4042

41-
if cmap in ['tab10']: n = 10
42-
if cmap in ['tab20', 'tab20b', 'tab20c']: n = 20
43-
if cmap in ['Pastel1', 'Set1']: n = 9
44-
if cmap in ['Pastel2', 'Accent', 'Dark2', 'Set2']: n = 8
45-
if cmap in ['Set3']: n = 12
46-
if cmap in ['Greys']: n = 11
47-
Z = np.linspace(0, 1, n).reshape(1, n)
48-
49-
ax.imshow(Z, extent=[xmin, xmax, ymin, ymax], cmap=plt.get_cmap(cmap))
43+
ax.imshow(Z, extent=[xmin, xmax, ymin, ymax], cmap=name)
5044
ax.set_xlim(xmin, xmax), ax.set_xticks([])
5145
ax.set_ylim(ymin, ymax), ax.set_yticks([])
5246

53-
"""
54-
if cmap == "tab10":
55-
x = np.linspace(xmin, xmax, 11, endpoint=True) + 0.5*(xmax-xmin)/11
56-
for i in range(10):
57-
ax.text(x[i], (ymin+ymax)/2, "C%d"%i, color="white", zorder=10,
58-
family = "Source Pro Serif", size=10, ha="center", va="center")
59-
60-
if cmap == "Greys":
61-
x = np.linspace(xmin, xmax, 12, endpoint=True) + 0.5*(xmax-xmin)/12
62-
for i in range(11):
63-
color = "%.1f"%(i/10)
64-
text = "%.1f"%(1-i/10)
65-
ax.text(x[i], (ymin+ymax)/2, text, color=color, zorder=10,
66-
family = "Source Pro Serif", size=10, ha="center", va="center")
67-
"""
68-
69-
fig.savefig(ROOT_DIR / f"figures/colormap-{cmap}.pdf")
47+
fig.savefig(ROOT_DIR / f"figures/colormap-{name}.pdf")
7048
ax.clear()

scripts/colors.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@
3333
for name, colors in palettes.items():
3434
C = mpl.colors.to_rgba_array(colors).reshape((1, len(colors), 4))
3535
ax.imshow(C, extent=[xmin, xmax, ymin, ymax])
36+
37+
# Drop alpha by assuming we're on a white background PDF.
38+
alpha = C[0, :, 3]
39+
rgb = C[0, :, :3] * alpha[:, np.newaxis] + (1 - alpha[:, np.newaxis])
40+
# Same calculation for luminance as
41+
# https://matplotlib.org/stable/users/explain/colors/colors.html#comparison-between-x11-css4-and-xkcd-colors
42+
luma = 0.299 * rgb[:, 0] + 0.587 * rgb[:, 1] + 0.114 * rgb[:, 2]
43+
3644
dx = (xmax-xmin)/len(colors)
3745
for i in range(len(colors)):
38-
color = "white"
39-
if colors[i] in ['1.0', 'w']: color = "black"
46+
text_color = "black" if luma[i] > 0.5 else "white"
4047
text = str(colors[i]).replace(' ', '')
41-
ax.text((i+0.5)*dx, (ymin+ymax)/2, text, color=color, zorder=10,
48+
ax.text((i+0.5)*dx, (ymin+ymax)/2, text, color=text_color, zorder=10,
4249
family="Source Code Pro", size=9, ha="center", va="center")
4350

4451
fig.savefig(ROOT_DIR / f"figures/colors-{name}.pdf")

0 commit comments

Comments
 (0)
0