Skip to content

Commit 3a02535

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 7df2184 commit 3a02535

11 files changed

+135
-97
lines changed

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

scripts/annotation-arrow-styles.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +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-
67

7-
def demo_con_style(ax, connectionstyle):
8-
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
9-
family="Source Code Pro",
10-
transform=ax.transAxes, ha="left", va="top", size="x-small")
8+
mpl.style.use([
9+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
10+
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
11+
])
1112

1213

13-
(fig, axes) = plt.subplots(5, 3, figsize=(4, 2.5), frameon=False)
14+
(fig, axes) = plt.subplots(5, 3, figsize=(5.7/2.54, 1.5), frameon=True)
1415
for ax in axes.flatten():
1516
ax.axis("off")
1617
for i, (ax, style) in enumerate(zip(axes.flatten(), mpatches.ArrowStyle.get_styles())):
@@ -21,13 +22,14 @@ def demo_con_style(ax, connectionstyle):
2122
xy=(x0, y0), xycoords='data',
2223
xytext=(x1, y1), textcoords='data',
2324
arrowprops=dict(arrowstyle=style,
25+
mutation_scale=10,
2426
color="black",
2527
shrinkA=5, shrinkB=5,
2628
patchA=None, patchB=None,
2729
connectionstyle="arc3,rad=0"))
2830
ax.text( (x1+x0)/2, y0-0.2, style,
2931
transform=ax.transAxes,
30-
family="Source Code Pro", ha="center", va="top")
32+
ha="center", va="top", fontsize=8)
3133

3234
plt.savefig("../figures/annotation-arrow-styles.pdf")
3335
# plt.show()
+14-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
import pathlib
2+
3+
import matplotlib as mpl
14
import matplotlib.pyplot as plt
25

36

7+
mpl.style.use([
8+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
9+
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
10+
])
11+
12+
413
def demo_con_style(ax, connectionstyle):
514
x1, y1 = 0.3, 0.2
615
x2, y2 = 0.8, 0.6
@@ -9,29 +18,26 @@ def demo_con_style(ax, connectionstyle):
918
xy=(x1, y1), xycoords='data',
1019
xytext=(x2, y2), textcoords='data',
1120
arrowprops=dict(arrowstyle="->", color="0.5",
12-
shrinkA=5, shrinkB=5,
21+
shrinkA=2, shrinkB=2,
1322
patchA=None, patchB=None,
1423
connectionstyle=connectionstyle),
1524
)
1625
ax.text(.05, .95, connectionstyle.replace(",", ",\n"),
17-
family="Source Code Pro",
18-
transform=ax.transAxes, ha="left", va="top", size="x-small")
19-
26+
transform=ax.transAxes, ha="left", va="top", size=4)
2027

21-
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))
2229
demo_con_style(axs[0, 0], "arc3,rad=0")
2330
demo_con_style(axs[0, 1], "arc3,rad=0.3")
2431
demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90")
2532
demo_con_style(axs[1, 0], "angle,angleA=-90,angleB=180,rad=0")
26-
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=25")
27-
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")
2835
demo_con_style(axs[2, 0], "bar,fraction=0.3")
2936
demo_con_style(axs[2, 1], "bar,fraction=-0.3")
3037
demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2")
3138

3239
for ax in axs.flat:
3340
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
34-
fig.tight_layout(pad=0.2)
3541

3642
plt.savefig("../figures/annotation-connection-styles.pdf")
3743
# plt.show()

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

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

scripts/projections.py

+15-12
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([])
35-
ax.set_ylim(0, 2)
36-
ax.grid(linewidth=1)
40+
ax.set_ylim(0,2)
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)

scripts/tick-formatters.py

+19-29
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@
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

1222

1323
def setup(ax):
14-
ax.spines['right'].set_color('none')
15-
ax.spines['left'].set_color('none')
1624
ax.yaxis.set_major_locator(ticker.NullLocator())
17-
ax.spines['top'].set_color('none')
18-
ax.xaxis.set_ticks_position('bottom')
19-
ax.tick_params(which='major', width=1.00, length=5)
20-
ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10)
2125
ax.set_xlim(0, 5)
2226
ax.set_ylim(0, 1)
2327
ax.patch.set_alpha(0.0)
2428

2529

26-
fig = plt.figure(figsize=(8, 5))
30+
fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54))
2731
fig.patch.set_alpha(0.0)
2832
n = 7
2933

30-
fontsize = 18
31-
family = "Source Code Pro"
32-
3334
# Null formatter
3435
ax = fig.add_subplot(n, 1, 1)
3536
setup(ax)
3637
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
3738
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
3839
ax.xaxis.set_major_formatter(ticker.NullFormatter())
3940
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
40-
ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family,
41-
fontsize=fontsize, transform=ax.transAxes)
41+
ax.text(0.0, 0.1, "ticker.NullFormatter()", transform=ax.transAxes)
4242

4343
# Fixed formatter
4444
ax = fig.add_subplot(n, 1, 2)
@@ -50,8 +50,7 @@ def setup(ax):
5050
minors = [""] + ["%.2f" % (x-int(x)) if (x-int(x))
5151
else "" for x in np.arange(0, 5, 0.25)]
5252
ax.xaxis.set_minor_formatter(ticker.FixedFormatter(minors))
53-
ax.text(0.0, 0.1, "ticker.FixedFormatter(['', '0', '1', ...])",
54-
family=family, fontsize=fontsize, transform=ax.transAxes)
53+
ax.text(0.0, 0.1, "ticker.FixedFormatter(['', '0', '1', ...])", transform=ax.transAxes)
5554

5655

5756
# FuncFormatter can be used as a decorator
@@ -65,8 +64,7 @@ def major_formatter(x, pos):
6564
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
6665
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
6766
ax.xaxis.set_major_formatter(major_formatter)
68-
ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)',
69-
family=family, fontsize=fontsize, transform=ax.transAxes)
67+
ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', transform=ax.transAxes)
7068

7169

7270
# FormatStr formatter
@@ -75,39 +73,31 @@ def major_formatter(x, pos):
7573
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
7674
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
7775
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<"))
78-
ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')",
79-
family=family, fontsize=fontsize, transform=ax.transAxes)
76+
ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", transform=ax.transAxes)
8077

8178
# Scalar formatter
8279
ax = fig.add_subplot(n, 1, 5)
8380
setup(ax)
8481
ax.xaxis.set_major_locator(ticker.AutoLocator())
8582
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
8683
ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
87-
ax.text(0.0, 0.1, "ticker.ScalarFormatter()",
88-
family=family, fontsize=fontsize, transform=ax.transAxes)
84+
ax.text(0.0, 0.1, "ticker.ScalarFormatter()", transform=ax.transAxes)
8985

9086
# StrMethod formatter
9187
ax = fig.add_subplot(n, 1, 6)
9288
setup(ax)
9389
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
9490
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
9591
ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}"))
96-
ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')",
97-
family=family, fontsize=fontsize, transform=ax.transAxes)
92+
ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", transform=ax.transAxes)
9893

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

112-
plt.savefig("../figures/tick-formatters.pdf", transparent=True)
102+
plt.savefig("../figures/tick-formatters.pdf")
113103
# plt.show()

0 commit comments

Comments
 (0)