8000 Simplify getting colormap sizes · matplotlib/cheatsheets@8551a49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8551a49

Browse files
committed
Simplify getting colormap sizes
All the special cases other than Greys are just the size of the colormap themselves, but we want to drop that special case as well. Also, colormaps are at most 256 elements long, so using `n=512` is probably not necessary.
1 parent 3fece66 commit 8551a49

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

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()

0 commit comments

Comments
 (0)
0