Skip to content

Commit dbf62a3

Browse files
committed
continue refactoring using style sheets
- add base style with constrained_layout enabled - add style for the ticks examples - add style for a grid of figures - resize figures to a maximum width of 57 mm - small tweaks to arrow connections to accommodate change in figure size
1 parent 755fdee commit dbf62a3

11 files changed

+137
-94
lines changed

Diff for: scripts/advanced-plots.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import matplotlib.pyplot as plt
1212

1313

14-
mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle')
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle',
17+
])
18+
1519

1620
fig = plt.figure()
1721
d = 0.01

Diff for: scripts/annotation-arrow-styles.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
import pathlib
2+
3+
import matplotlib as mpl
14
import matplotlib.pyplot as plt
25
import matplotlib.patches as mpatches
36

4-
styles = mpatches.ArrowStyle.get_styles()
5-
def demo_con_style(ax, connectionstyle):
6-
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
7-
family="Source Code Pro",
8-
transform=ax.transAxes, ha="left", va="top", size="x-small")
97

10-
(fig, axes) = plt.subplots(5, 3, figsize=(4, 2.5), frameon=False)
8+
mpl.style.use([
9+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
10+
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
11+
])
12+
13+
14+
(fig, axes) = plt.subplots(5, 3, figsize=(5.7/2.54, 1.5), frameon=True)
1115
for ax in axes.flatten():
1216
ax.axis("off")
1317
for i,(ax,style) in enumerate(zip(axes.flatten(), mpatches.ArrowStyle.get_styles())):
@@ -18,13 +22,14 @@ def demo_con_style(ax, connectionstyle):
1822
xy=(x0, y0), xycoords='data',
1923
xytext=(x1, y1), textcoords='data',
2024
arrowprops=dict(arrowstyle=style,
25+
mutation_scale=10,
2126
color="black",
2227
shrinkA=5, shrinkB=5,
2328
patchA=None, patchB=None,
2429
connectionstyle="arc3,rad=0"))
2530
ax.text( (x1+x0)/2, y0-0.2, style,
2631
transform=ax.transAxes,
27-
family = "Source Code Pro", ha="center", va="top")
32+
ha="center", va="top", fontsize=8)
2833

2934
plt.savefig("../figures/annotation-arrow-styles.pdf")
3035
# plt.show()

Diff for: scripts/annotation-connection-styles.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
import pathlib
2+
3+
import matplotlib as mpl
14
import matplotlib.pyplot as plt
25

6+
7+
mpl.style.use([
8+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
9+
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
10+
])
11+
12+
313
def demo_con_style(ax, connectionstyle):
414
x1, y1 = 0.3, 0.2
515
x2, y2 = 0.8, 0.6
@@ -8,28 +18,26 @@ def demo_con_style(ax, connectionstyle):
818
xy=(x1, y1), xycoords='data',
919
xytext=(x2, y2), textcoords='data',
1020
arrowprops=dict(arrowstyle="->", color="0.5",
11-
shrinkA=5, shrinkB=5,
21+
shrinkA=2, shrinkB=2,
1222
patchA=None, patchB=None,
1323
connectionstyle=connectionstyle),
1424
)
1525
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
16-
family="Source Code Pro",
17-
transform=ax.transAxes, ha="left", va="top", size="x-small")
26+
transform=ax.transAxes, ha="left", va="top", size=4)
1827

19-
fig, axs = plt.subplots(3, 3, figsize=(5, 5))
28+
fig, axs = plt.subplots(3, 3, figsize=(5.7/2.54, 5.7/2.54))
2029
demo_con_style(axs[0, 0], "arc3,rad=0")
2130
demo_con_style(axs[0, 1], "arc3,rad=0.3")
2231
demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90")
2332
demo_con_style(axs[1, 0], "angle,angleA=-90,angleB=180,rad=0")
24-
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=25")
25-
demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0")
33+
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=10")
34+
demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=20,rad=0")
2635
demo_con_style(axs[2, 0], "bar,fraction=0.3")
2736
demo_con_style(axs[2, 1], "bar,fraction=-0.3")
2837
demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2")
2938

3039
for ax in axs.flat:
3140
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
32-
fig.tight_layout(pad=0.2)
3341

3442
plt.savefig("../figures/annotation-connection-styles.pdf")
3543
# plt.show()

Diff for: scripts/basic-plots.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import matplotlib.pyplot as plt
1212

1313

14-
mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle')
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle',
17+
])
18+
1519

1620
fig = plt.figure()
1721
d = 0.01

Diff for: scripts/interpolations.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import pathlib
2+
3+
import matplotlib as mpl
14
import matplotlib.pyplot as plt
25
import numpy as np
36

7+
8+
mpl.style.use([
9+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
10+
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
11+
])
12+
13+
414
methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16',
515
'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric',
616
'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos']
@@ -9,14 +19,14 @@
919
Z = np.random.uniform(0,1,(3,3))
1020

1121

12-
fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(4.5,9),
22+
fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(5.7/2.54,5.7/2.54*2),
23+
# fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(4.5,9),
1324
subplot_kw={'xticks': [], 'yticks': []})
1425
for ax, interp_method in zip(axs.flat, methods):
1526
ax.imshow(Z, interpolation=interp_method, cmap='viridis',
16-
extent=[0,9,0,9], rasterized=True)
17-
ax.text(4.5, 1, str(interp_method), weight="bold", color="white", size=12,
18-
transform=ax.transData, ha="center", va="center")
27+
extent=[0,1,0,1], rasterized=True)
28+
ax.text(0.5, 0.1, str(interp_method), weight="bold", color="white", size=6,
29+
ha="center", va="center")
1930

20-
plt.tight_layout()
2131
plt.savefig("../figures/interpolations.pdf", dpi=600)
2232
# plt.show()

Diff for: scripts/projections.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
import pathlib
6+
57
import numpy as np
68
import cartopy
79
import matplotlib as mpl
810
import matplotlib.pyplot as plt
911

1012

13+
mpl.style.use([
14+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
15+
pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle',
16+
])
17+
18+
1119
CARTOPY_SOURCE_TEMPLATE = 'https://naturalearth.s3.amazonaws.com/{resolution}_{category}/ne_{resolution}_{name}.zip'
1220

1321

@@ -21,26 +29,22 @@
2129

2230
# Polar plot
2331
# -----------------------------------------------------------------------------
24-
mpl.rc('axes', linewidth=4.0)
25-
fig = plt.figure(figsize=(4,4))
26-
b = 0.025
27-
ax = fig.add_axes([b,b,1-2*b,1-2*b], projection="polar")
32+
(fig, ax) = plt.subplots(subplot_kw={'projection': 'polar'})
2833
T = np.linspace(0, 3*2*np.pi, 500)
2934
R = np.linspace(0, 2, len(T))
30-
ax.plot(T, R, color="C1", linewidth=6)
35+
ax.plot(T, R, color="C1")
3136
ax.set_xticks(np.linspace(0, 2*np.pi, 2*8))
3237
ax.set_xticklabels([])
3338
ax.set_yticks(np.linspace(0, 2, 8))
3439
ax.set_yticklabels([])
3540
ax.set_ylim(0,2)
36-
ax.grid(linewidth=1)
41+
ax.grid(linewidth=0.2)
3742
plt.savefig("../figures/projection-polar.pdf")
3843
fig.clear()
3944

4045
# 3D plot
4146
# -----------------------------------------------------------------------------
42-
mpl.rc('axes', linewidth=2.0)
43-
ax = fig.add_axes([0,.1,1,.9], projection="3d")
47+
(fig, ax) = plt.subplots(subplot_kw={'projection': '3d'})
4448
r = np.linspace(0, 1.25, 50)
4549
p = np.linspace(0, 2*np.pi, 50)
4650
R, P = np.meshgrid(r, p)
@@ -58,9 +62,8 @@
5862

5963
# Cartopy plot
6064
# -----------------------------------------------------------------------------
61-
mpl.rc('axes', linewidth=3.0)
62-
b = 0.025
63-
ax = fig.add_axes([b,b,1-2*b,1-2*b], frameon=False,
65+
fig = plt.figure()
66+
ax = fig.add_subplot(frameon=False,
6467
projection=cartopy.crs.Orthographic())
6568
ax.add_feature(cartopy.feature.LAND, zorder=0,
6669
facecolor="C1", edgecolor="0.0", linewidth=0)

Diff for: scripts/tick-formatters.py

+19-29
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,40 @@
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
import matplotlib.ticker as ticker
912

13+
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
pathlib.Path(__file__).parent/'../styles/ticks.mplstyle',
17+
])
18+
19+
1020
# Setup a plot such that only the bottom spine is shown
1121
def setup(ax):
12-
ax.spines['right'].set_color('none')
13-
ax.spines['left'].set_color('none')
1422
ax.yaxis.set_major_locator(ticker.NullLocator())
15-
ax.spines['top'].set_color('none')
16-
ax.xaxis.set_ticks_position('bottom')
17-
ax.tick_params(which='major', width=1.00, length=5)
18-
ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10)
1923
ax.set_xlim(0, 5)
2024
ax.set_ylim(0, 1)
2125
ax.patch.set_alpha(0.0)
2226

2327

24-
fig = plt.figure(figsize=(8, 5))
28+
fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54))
2529
fig.patch.set_alpha(0.0)
2630
n = 7
2731

28-
fontsize = 18
29-
family = "Source Code Pro"
30-
3132
# Null formatter
3233
ax = fig.add_subplot(n, 1, 1)
3334
setup(ax)
3435
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
3536
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
3637
ax.xaxis.set_major_formatter(ticker.NullFormatter())
3738
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
38-
ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family,
39-
fontsize=fontsize, transform=ax.transAxes)
39+
ax.text(0.0, 0.1, "ticker.NullFormatter()", transform=ax.transAxes)
4040

4141
# Fixed formatter
4242
ax = fig.add_subplot(n, 1, 2)
@@ -48,8 +48,7 @@ def setup(ax):
4848
minors = [""] + ["%.2f" % (x-int(x)) if (x-int(x))
4949
else "" for x in np.arange(0, 5, 0.25)]
5050
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(minors))
51-
ax.text(0.0, 0.1, "ticker.FixedFormatter(['', '0', '1', ...])",
52-
family=family, fontsize=fontsize, transform=ax.transAxes)
51+
ax.text(0.0, 0.1, "ticker.FixedFormatter(['', '0', '1', ...])", transform=ax.transAxes)
5352

5453

5554
# FuncFormatter can be used as a decorator
@@ -63,8 +62,7 @@ def major_formatter(x, pos):
6362
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
6463
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
6564
ax.xaxis.set_major_formatter(major_formatter)
66-
ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)',
67-
family=family, fontsize=fontsize, transform=ax.transAxes)
65+
ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', transform=ax.transAxes)
6866

6967

7068
# FormatStr formatter
@@ -73,39 +71,31 @@ def major_formatter(x, pos):
7371
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
7472
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
7573
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<"))
76-
ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')",
77-
family=family, fontsize=fontsize, transform=ax.transAxes)
74+
ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", transform=ax.transAxes)
7875

7976
# Scalar formatter
8077
ax = fig.add_subplot(n, 1, 5)
8178
setup(ax)
8279
ax.xaxis.set_major_locator(ticker.AutoLocator())
8380
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
8481
ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
85-
ax.text(0.0, 0.1, "ticker.ScalarFormatter()",
86-
family=family, fontsize=fontsize, transform=ax.transAxes)
82+
ax.text(0.0, 0.1, "ticker.ScalarFormatter()", transform=ax.transAxes)
8783

8884
# StrMethod formatter
8985
ax = fig.add_subplot(n, 1, 6)
9086
setup(ax)
9187
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
9288
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
9389
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}"))
94-
ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')",
95-
family=family, fontsize=fontsize, transform=ax.transAxes)
90+
ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", transform=ax.transAxes)
9691

9792
# Percent formatter
9893
ax = fig.add_subplot(n, 1, 7)
9994
setup(ax)
10095
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
10196
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
10297
ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5))
103-
ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)",
104-
family=family, fontsize=fontsize, transform=ax.transAxes)
105-
106-
# Push the top of the top axes outside the figure because we only show the
107-
# bottom spine.
108-
fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05)
98+
ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", transform=ax.transAxes)
10999

110-
plt.savefig("../figures/tick-formatters.pdf", transparent=True)
100+
plt.savefig("../figures/tick-formatters.pdf")
111101
# plt.show()

0 commit comments

Comments
 (0)