Skip to content

Commit 3b530c1

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 097e9e4 commit 3b530c1

11 files changed

+136
-96
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

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

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

+14-7
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,27 @@ 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")
26+
transform=ax.transAxes, ha="left", va="top", size=4)
1927

2028

21-
fig, axs = plt.subplots(3, 3, figsize=(5, 5))
29+
fig, axs = plt.subplots(3, 3, figsize=(5.7/2.54, 5.7/2.54))
2230
demo_con_style(axs[0, 0], "arc3,rad=0")
2331
demo_con_style(axs[0, 1], "arc3,rad=0.3")
2432
demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90")
2533
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")
34+
demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=10")
35+
demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=20,rad=0")
2836
demo_con_style(axs[2, 0], "bar,fraction=0.3")
2937
demo_con_style(axs[2, 1], "bar,fraction=-0.3")
3038
demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2")
3139

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

3643
plt.savefig("../figures/annotation-connection-styles.pdf")
3744
# 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

+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([])
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,10 +62,9 @@
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,
64-
projection=cartopy.crs.Orthographic())
65+
fig = plt.figure()
66+
ax = fig.add_subplot(frameon=False,
67+
projection=cartopy.crs.Orthographic())
6568
ax.add_feature(cartopy.feature.LAND, zorder=0,
6669
facecolor="C1", edgecolor="0.0", linewidth=0)
6770
plt.savefig("../figures/projection-cartopy.pdf")

Diff for: scripts/tick-formatters.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,43 @@
33
# Author: Nicolas P. Rougier
44
# License: BSD
55
# ----------------------------------------------------------------------------
6+
import pathlib
7+
8+
import numpy as np
9+
import matplotlib as mpl
610
import matplotlib.pyplot as plt
711
import matplotlib.ticker as ticker
812

13+
14+
mpl.style.use([
15+
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
16+
pathlib.Path(__file__).parent/'../styles/ticks.mplstyle',
17+
])
18+
19+
920
# Setup a plot such that only the bottom spine is shown
1021

1122

1223
def setup(ax):
1324
"""Set up Axes with just an x-Axis."""
14-
ax.spines['right'].set_color('none')
15-
ax.spines['left'].set_color('none')
1625
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)
2126
ax.set_xlim(0, 5)
2227
ax.set_ylim(0, 1)
2328
ax.patch.set_alpha(0.0)
2429

2530

26-
fig = plt.figure(figsize=(8, 5))
31+
fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54))
2732
fig.patch.set_alpha(0.0)
2833
n = 7
2934

30-
fontsize = 18
31-
family = "Source Code Pro"
32-
3335
# Null formatter
3436
ax = fig.add_subplot(n, 1, 1)
3537
setup(ax)
3638
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
3739
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
3840
ax.xaxis.set_major_formatter(ticker.NullFormatter())
3941
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
40-
ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family,
41-
fontsize=fontsize, transform=ax.transAxes)
42+
ax.text(0.0, 0.1, "ticker.NullFormatter()", transform=ax.transAxes)
4243

4344
# Fixed formatter
4445
ax = fig.add_subplot(n, 1, 2)
@@ -47,8 +48,7 @@ def setup(ax):
4748
ax.xaxis.set_major_locator(ticker.FixedLocator(range(6)))
4849
majors = ["zero", "one", "two", "three", "four", "five"]
4950
ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors))
50-
ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])",
51-
family=family, fontsize=fontsize, transform=ax.transAxes)
51+
ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", transform=ax.transAxes)
5252

5353

5454
# FuncFormatter can be used as a decorator
@@ -63,8 +63,7 @@ def major_formatter(x, pos):
6363
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
6464
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
6565
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)
66+
ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', transform=ax.transAxes)
6867

6968

7069
# FormatStr formatter
@@ -73,39 +72,31 @@ def major_formatter(x, pos):
7372
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
7473
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
7574
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)
75+
ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", transform=ax.transAxes)
7876

7977
# Scalar formatter
8078
ax = fig.add_subplot(n, 1, 5)
8179
setup(ax)
8280
ax.xaxis.set_major_locator(ticker.AutoLocator())
8381
ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
8482
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)
83+
ax.text(0.0, 0.1, "ticker.ScalarFormatter()", transform=ax.transAxes)
8784

8885
# StrMethod formatter
8986
ax = fig.add_subplot(n, 1, 6)
9087
setup(ax)
9188
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
9289
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
9390
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)
91+
ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", transform=ax.transAxes)
9692

9793
# Percent formatter
9894
ax = fig.add_subplot(n, 1, 7)
9995
setup(ax)
10096
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
10197
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
10298
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)
99+
ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", transform=ax.transAxes)
109100

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

0 commit comments

Comments
 (0)