Skip to content

Commit 73ced9f

Browse files
committed
finish (for now) refactoring using style sheets
- diminish scope of pdfcrop use - migrate more scripts to use the base mplstyle - continue migration to subplots usage - refactor to use common subplot_kw where possible - add style sheet for the sine plots - make more DRY
1 parent 3b530c1 commit 73ced9f

18 files changed

+378
-402
lines changed

Diff for: Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ logos:
1414
figures:
1515
# generate the figures
1616
cd scripts && for script in *.py; do echo $$script; MPLBACKEND="agg" python $$script; done
17-
# crop the figures
18-
cd figures && for figure in *.pdf; do echo $$figure; pdfcrop $$figure $$figure; done
19-
# regenerate some figures that should not be cropped
20-
cd scripts && MPLBACKEND="agg" python styles.py
17+
# crop some of the figures
18+
cd figures && pdfcrop adjustments.pdf adjustments.pdf
19+
cd figures && pdfcrop annotate.pdf annotate.pdf
20+
cd figures && pdfcrop anatomy.pdf anatomy.pdf
21+
cd figures && pdfcrop colornames.pdf colornames.pdf
22+
cd figures && pdfcrop fonts.pdf fonts.pdf
23+
cd figures && pdfcrop markers.pdf markers.pdf
24+
cd figures && pdfcrop text-alignments.pdf text-alignments.pdf
25+
cd figures && pdfcrop tip-font-family.pdf tip-font-family.pdf
26+
cd figures && pdfcrop tip-hatched.pdf tip-hatched.pdf
2127

2228
.PHONY: cheatsheets
2329
cheatsheets:

Diff for: scripts/adjustements.py

+45-26
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,82 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import pathlib
56

67
import numpy as np
8+
import matplotlib as mpl
79
import matplotlib.pyplot as plt
810
import matplotlib.patches as mpatches
911
from matplotlib.collections import PatchCollection
1012

1113

12-
fig = plt.figure(figsize=(4.25, 4.25 * 95/115))
13-
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1,
14-
xlim=(0-5, 100+10), ylim=(-10, 80+5), xticks=[], yticks=[])
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
])
17+
mpl.rc('font', size=4)
18+
mpl.rc('lines', linewidth=0.5)
19+
mpl.rc('patch', linewidth=0.5)
20+
21+
22+
subplots_kw = dict(
23+
figsize=(5.7/2.54, 5.7/2.54 * 95/115),
24+
subplot_kw=dict(
25+
frameon=False,
26+
aspect=1,
27+
xlim=(0-5, 100+10),
28+
ylim=(-10, 80+5),
29+
xticks=[],
30+
yticks=[],
31+
),
32+
)
33+
34+
(fig, ax) = plt.subplots(**subplots_kw)
1535

1636

1737
box = mpatches.FancyBboxPatch(
1838
(0, 0), 100, 83, mpatches.BoxStyle("Round", pad=0, rounding_size=2),
19-
linewidth=1., facecolor="0.9", edgecolor="black")
39+
facecolor="0.9", edgecolor="black")
2040
ax.add_artist(box)
2141

2242
box = mpatches.FancyBboxPatch(
2343
(0, 0), 100, 75, mpatches.BoxStyle("Round", pad=0, rounding_size=0),
24-
linewidth=1., facecolor="white", edgecolor="black")
44+
facecolor="white", edgecolor="black")
2545
ax.add_artist(box)
2646

2747

2848
box = mpatches.Rectangle(
2949
(5, 5), 45, 30, zorder=10,
30-
linewidth=1.0, facecolor="white", edgecolor="black")
50+
facecolor="white", edgecolor="black")
3151
ax.add_artist(box)
3252

3353
box = mpatches.Rectangle(
3454
(5, 40), 45, 30, zorder=10,
35-
linewidth=1.0, facecolor="white", edgecolor="black")
55+
facecolor="white", edgecolor="black")
3656
ax.add_artist(box)
3757

3858
box = mpatches.Rectangle(
3959
(55, 5), 40, 65, zorder=10,
40-
linewidth=1.0, facecolor="white", edgecolor="black")
60+
facecolor="white", edgecolor="black")
4161
ax.add_artist(box)
4262

4363
# Window button
4464
X, Y = [5, 10, 15], [79, 79, 79]
45-
plt.scatter(X, Y, s=75, zorder=10,
46-
edgecolor="black", facecolor="white", linewidth=1)
65+
plt.scatter(X, Y, s=20, zorder=10,
66+
edgecolor="black", facecolor="white")
4767

4868

4969
# Window size extension
5070
X, Y = [0, 0], [0, -8]
51-
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False)
71+
plt.plot(X, Y, color="black", linestyle=":", clip_on=False)
5272

5373
X, Y = [100, 100], [0, -8]
54-
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False)
74+
plt.plot(X, Y, color="black", linestyle=":", clip_on=False)
5575

5676
X, Y = [100, 108], [0, 0]
57-
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False)
77+
plt.plot(X, Y, color="black", linestyle=":", clip_on=False)
5878

5979
X, Y = [100, 108], [75, 75]
60-
plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False)
80+
plt.plot(X, Y, color="black", linestyle=":", clip_on=False)
6181

6282

6383
def ext_arrow(p0, p1, p2, p3):
@@ -69,7 +89,7 @@ def ext_arrow(p0, p1, p2, p3):
6989
ax.arrow(*p3, *(p2-p3), zorder=20, linewidth=0,
7090
length_includes_head=True, width=.4,
7191
head_width=2, head_length=2, color="black")
72-
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.9, color="black")
92+
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.5, color="black")
7393

7494

7595
def int_arrow(p0, p1):
@@ -85,45 +105,44 @@ def int_arrow(p0, p1):
85105
x = 0
86106
y = 10
87107
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) )
88-
ax.text(x+9.5, y, "left", ha="left", va="center", size="x-small", zorder=20)
108+
ax.text(x+9.5, y, "left", ha="left", va="center", zorder=20)
89109

90110
x += 50
91111
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) )
92-
ax.text(x-4.5, y, "wspace", ha="right", va="center", size="x-small", zorder=20)
112+
ax.text(x-4.5, y, "wspace", ha="right", va="center", zorder=20)
93113

94114
x += 45
95115
ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) )
96-
ax.text(x-4.5, y, "right", ha="right", va="center", size="x-small", zorder=20)
116+
ax.text(x-4.5, y, "right", ha="right", va="center", zorder=20)
97117

98118
y = 0
99119
x = 25
100120
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) )
101-
ax.text(x, y+9.5, "bottom", ha="center", va="bottom", size="x-small", zorder=20)
121+
ax.text(x, y+9.5, "bottom", ha="center", va="bottom", zorder=20)
102122

103123
y += 35
104124
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) )
105-
ax.text(x, y-4.5, "hspace", ha="center", va="top", size="x-small", zorder=20)
125+
ax.text(x, y-4.5, "hspace", ha="center", va="top", zorder=20)
106126

107127
y += 35
108128
ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) )
109-
ax.text(x, y-4.5, "top", ha="center", va="top", size="x-small", zorder=20)
129+
ax.text(x, y-4.5, "top", ha="center", va="top", zorder=20)
110130

111131
int_arrow((0, -5), (100, -5))
112132
ax.text(50, -5, "figure width", backgroundcolor="white", zorder=30,
113-
ha="center", va="center", size="x-small")
133+
ha="center", va="center")
114134

115135
int_arrow((105, 0), (105, 75))
116136
ax.text(105, 75/2, "figure height", backgroundcolor="white", zorder=30,
117-
rotation="vertical", ha="center", va="center", size="x-small")
137+
rotation="vertical", ha="center", va="center")
118138

119139
int_arrow((55, 62.5), (95, 62.5))
120140
ax.text(75, 62.5, "axes width", backgroundcolor="white", zorder=30,
121-
ha="center", va="center", size="x-small")
141+
ha="center", va="center")
122142

123143
int_arrow((62.5, 5), (62.5, 70))
124144
ax.text(62.5, 35, "axes height", backgroundcolor="white", zorder=30,
125-
rotation="vertical", ha="center", va="center", size="x-small")
145+
rotation="vertical", ha="center", va="center")
126146

127147

128148
plt.savefig("../figures/adjustments.pdf")
129-
# plt.show()

Diff for: scripts/advanced-plots.py

+31-45
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,37 @@
1717
])
1818

1919

20-
fig = plt.figure()
21-
d = 0.01
22-
ax = fig.add_axes([d, d, 1-2*d, 1-2*d])
20+
subplot_kw = dict(
21+
xlim=(0, 8), xticks=np.arange(1, 8),
22+
ylim=(0, 8), yticks=np.arange(1, 8),
23+
)
2324

2425
# Step plot
2526
# -----------------------------------------------------------------------------
27+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
2628
X = np.linspace(0, 10, 16)
2729
Y = 4 + 2*np.sin(2*X)
2830
ax.step(X, Y, color="C1")
29-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
30-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
31-
ax.grid(linewidth=0.125)
32-
plt.savefig("../figures/advanced-step.pdf")
33-
ax.clear()
31+
ax.grid()
32+
fig.savefig("../figures/advanced-step.pdf")
3433

3534
# Violin plot
3635
# -----------------------------------------------------------------------------
36+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
3737
np.random.seed(10)
3838
D = np.random.normal((3, 5, 4), (0.75, 1.00, 0.75), (200, 3))
3939
VP = ax.violinplot(D, [2, 4, 6], widths=1.5,
4040
showmeans=False, showmedians=False, showextrema=False)
4141
for body in VP['bodies']:
4242
body.set_facecolor('C1')
4343
body.set_alpha(1)
44-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
45-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
4644
ax.set_axisbelow(True)
47-
ax.grid(linewidth=0.125)
48-
plt.savefig("../figures/advanced-violin.pdf")
49-
ax.clear()
45+
ax.grid()
46+
fig.savefig("../figures/advanced-violin.pdf")
5047

5148
# Boxplot
5249
# -----------------------------------------------------------------------------
50+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
5351
np.random.seed(10)
5452
D = np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3))
5553
VP = ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True,
@@ -63,85 +61,74 @@
6361
"linewidth": 0.75},
6462
capprops={"color": "C1",
6563
"linewidth": 0.75})
66-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
67-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
6864
ax.set_axisbelow(True)
69-
ax.grid(linewidth=0.125)
70-
plt.savefig("../figures/advanced-boxplot.pdf")
71-
ax.clear()
65+
ax.grid()
66+
fig.savefig("../figures/advanced-boxplot.pdf")
7267

7368
# Barbs plot
7469
# -----------------------------------------------------------------------------
70+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
7571
np.random.seed(1)
7672
X = [[2, 4, 6]]
7773
Y = [[1.5, 3, 2]]
7874
U = -np.ones((1, 3)) * 0
7975
V = -np.ones((1, 3)) * np.linspace(50, 100, 3)
8076
ax.barbs(X, Y, U, V, barbcolor="C1", flagcolor="C1", length=5, linewidth=0.5)
81-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
82-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
8377
ax.set_axisbelow(True)
84-
ax.grid(linewidth=0.125)
85-
plt.savefig("../figures/advanced-barbs.pdf")
86-
ax.clear()
78+
ax.grid()
79+
fig.savefig("../figures/advanced-barbs.pdf")
8780

8881
# Event plot
8982
# -----------------------------------------------------------------------------
83+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
9084
np.random.seed(1)
9185
X = [2, 4, 6]
9286
D = np.random.gamma(4, size=(3, 50))
9387
ax.eventplot(D, colors="C1", orientation="vertical", lineoffsets=X,
9488
linewidth=0.25)
95-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
96-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
9789
ax.set_axisbelow(True)
98-
ax.grid(linewidth=0.125)
99-
plt.savefig("../figures/advanced-event.pdf")
100-
ax.clear()
90+
ax.grid()
91+
fig.savefig("../figures/advanced-event.pdf")
10192

10293
# Errorbar plot
10394
# -----------------------------------------------------------------------------
95+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
10496
np.random.seed(1)
10597
X = [2, 4, 6]
10698
Y = [4, 5, 4]
10799
E = np.random.uniform(0.5, 1.5, 3)
108100
ax.errorbar(X, Y, E, color="C1", linewidth=0.75, capsize=1)
109-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
110-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
111101
ax.set_axisbelow(True)
112-
ax.grid(linewidth=0.125)
113-
plt.savefig("../figures/advanced-errorbar.pdf")
114-
ax.clear()
102+
ax.grid()
103+
fig.savefig("../figures/advanced-errorbar.pdf")
115104

116105
# Hexbin plot
117106
# -----------------------------------------------------------------------------
107+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
118108
np.random.seed(1)
119109
X = np.random.uniform(1.5, 6.5, 100)
120110
Y = np.random.uniform(1.5, 6.5, 100)
121111
C = np.random.uniform(0, 1, 10000)
122112
ax.hexbin(X, Y, C, gridsize=4, linewidth=0.25, edgecolor="white",
123113
cmap=plt.get_cmap("Wistia"), alpha=1.0)
124-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
125-
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
126114
ax.set_axisbelow(True)
127-
ax.grid(linewidth=0.125)
128-
plt.savefig("../figures/advanced-hexbin.pdf")
129-
ax.clear()
115+
ax.grid()
116+
fig.savefig("../figures/advanced-hexbin.pdf")
130117

131118
# Hist plot
132119
# -----------------------------------------------------------------------------
120+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
133121
np.random.seed(1)
134122
X = 4 + np.random.normal(0, 1.5, 200)
135123
ax.hist(X, bins=8, facecolor="C1", linewidth=0.25, edgecolor="white")
136-
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
137124
ax.set_ylim(0, 80), ax.set_yticks(np.arange(1, 80, 10))
138125
ax.set_axisbelow(True)
139-
ax.grid(linewidth=0.125)
140-
plt.savefig("../figures/advanced-hist.pdf")
141-
ax.clear()
126+
ax.grid()
127+
fig.savefig("../figures/advanced-hist.pdf")
142128

143129
# Xcorr plot
144130
# -----------------------------------------------------------------------------
131+
(fig, ax) = plt.subplots(subplot_kw=subplot_kw)
145132
np.random.seed(3)
146133
Y = np.random.uniform(-4, 4, 250)
147134
X = np.random.uniform(-4, 4, 250)
@@ -150,6 +137,5 @@
150137
ax.set_xlim(-8, 8), ax.set_xticks(np.arange(-8, 8, 2))
151138
ax.set_ylim(-.25, .25), ax.set_yticks(np.linspace(-.25, .25, 9))
152139
ax.set_axisbelow(True)
153-
ax.grid(linewidth=0.125)
154-
plt.savefig("../figures/advanced-xcorr.pdf")
155-
ax.clear()
140+
ax.grid()
141+
fig.savefig("../figures/advanced-xcorr.pdf")

Diff for: scripts/anatomy.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
# Author: Nicolas P. Rougier
44
# License: BSD
55
# ----------------------------------------------------------------------------
6+
import pathlib
7+
68
import numpy as np
9+
import matplotlib as mpl
710
import matplotlib.pyplot as plt
811
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
912

13+
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
])
17+
18+
1019
np.random.seed(123)
1120

1221
X = np.linspace(0.5, 3.5, 100)
1322
Y1 = 3+np.cos(X)
1423
Y2 = 1+np.cos(1+X/0.75)/2
1524
Y3 = np.random.uniform(Y1, Y2, len(X))
1625

17-
fig = plt.figure(figsize=(8, 8))
18-
ax = fig.add_subplot(1, 1, 1, aspect=1)
26+
(fig, ax) = plt.subplots(figsize=(8, 8), subplot_kw=dict(aspect=1))
1927

2028

2129
def minor_tick(x, pos):

0 commit comments

Comments
 (0)