Skip to content

Commit 3c816d7

Browse files
committed
Save figures in a path relative to the script
This allows running them from the `scripts` directory, _or_ the top of the checkout.
1 parent 36607cd commit 3c816d7

37 files changed

+242
-109
lines changed

Diff for: scripts/adjustements.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from matplotlib.collections import PatchCollection
1212

1313

14+
ROOT_DIR = pathlib.Path(__file__).parent.parent
15+
1416
mpl.style.use([
15-
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
17+
ROOT_DIR / 'styles/base.mplstyle',
1618
])
1719
mpl.rc('font', size=4)
1820
mpl.rc('lines', linewidth=0.5)
@@ -33,7 +35,6 @@
3335

3436
(fig, ax) = plt.subplots(**subplots_kw)
3537

36-
3738
box = mpatches.FancyBboxPatch(
3839
(0, 0), 100, 83, mpatches.BoxStyle("Round", pad=0, rounding_size=2),
3940
facecolor="0.9", edgecolor="black")
@@ -145,4 +146,4 @@ def int_arrow(p0, p1):
145146
rotation="vertical", ha="center", va="center")
146147

147148

148-
plt.savefig("../figures/adjustments.pdf")
149+
fig.savefig(ROOT_DIR / "figures/adjustments.pdf")

Diff for: scripts/advanced-plots.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import matplotlib.pyplot as plt
1212

1313

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

1921

@@ -29,7 +31,7 @@
2931
Y = 4 + 2*np.sin(2*X)
3032
ax.step(X, Y, color="C1")
3133
ax.grid()
32-
fig.savefig("../figures/advanced-step.pdf")
34+
fig.savefig(ROOT_DIR / "figures/advanced-step.pdf")
3335

3436
# Violin plot
3537
# -----------------------------------------------------------------------------
@@ -43,7 +45,7 @@
4345
body.set_alpha(1)
4446
ax.set_axisbelow(True)
4547
ax.grid()
46-
fig.savefig("../figures/advanced-violin.pdf")
48+
fig.savefig(ROOT_DIR / "figures/advanced-violin.pdf")
4749

4850
# Boxplot
4951
# -----------------------------------------------------------------------------
@@ -63,7 +65,7 @@
6365
"linewidth": 0.75})
6466
ax.set_axisbelow(True)
6567
ax.grid()
66-
fig.savefig("../figures/advanced-boxplot.pdf")
68+
fig.savefig(ROOT_DIR / "figures/advanced-boxplot.pdf")
6769

6870
# Barbs plot
6971
# -----------------------------------------------------------------------------
@@ -76,7 +78,7 @@
7678
ax.barbs(X, Y, U, V, barbcolor="C1", flagcolor="C1", length=5, linewidth=0.5)
7779
ax.set_axisbelow(True)
7880
ax.grid()
79-
fig.savefig("../figures/advanced-barbs.pdf")
81+
fig.savefig(ROOT_DIR / "figures/advanced-barbs.pdf")
8082

8183
# Event plot
8284
# -----------------------------------------------------------------------------
@@ -88,7 +90,7 @@
8890
linewidth=0.25)
8991
ax.set_axisbelow(True)
9092
ax.grid()
91-
fig.savefig("../figures/advanced-event.pdf")
93+
fig.savefig(ROOT_DIR / "figures/advanced-event.pdf")
9294

9395
# Errorbar plot
9496
# -----------------------------------------------------------------------------
@@ -100,7 +102,7 @@
100102
ax.errorbar(X, Y, E, color="C1", linewidth=0.75, capsize=1)
101103
ax.set_axisbelow(True)
102104
ax.grid()
103-
fig.savefig("../figures/advanced-errorbar.pdf")
105+
fig.savefig(ROOT_DIR / "figures/advanced-errorbar.pdf")
104106

105107
# Hexbin plot
106108
# -----------------------------------------------------------------------------
@@ -113,7 +115,7 @@
113115
cmap=plt.get_cmap("Wistia"), alpha=1.0)
114116
ax.set_axisbelow(True)
115117
ax.grid()
116-
fig.savefig("../figures/advanced-hexbin.pdf")
118+
fig.savefig(ROOT_DIR / "figures/advanced-hexbin.pdf")
117119

118120
# Hist plot
119121
# -----------------------------------------------------------------------------
@@ -124,7 +126,7 @@
124126
ax.set_ylim(0, 80), ax.set_yticks(np.arange(1, 80, 10))
125127
ax.set_axisbelow(True)
126128
ax.grid()
127-
fig.savefig("../figures/advanced-hist.pdf")
129+
fig.savefig(ROOT_DIR / "figures/advanced-hist.pdf")
128130

129131
# Xcorr plot
130132
# -----------------------------------------------------------------------------
@@ -138,4 +140,4 @@
138140
ax.set_ylim(-.25, .25), ax.set_yticks(np.linspace(-.25, .25, 9))
139141
ax.set_axisbelow(True)
140142
ax.grid()
141-
fig.savefig("../figures/advanced-xcorr.pdf")
143+
fig.savefig(ROOT_DIR / "figures/advanced-xcorr.pdf")

Diff for: scripts/anatomy.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
1212

1313

14+
ROOT_DIR = pathlib.Path(__file__).parent.parent
15+
1416
mpl.style.use([
15-
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
17+
ROOT_DIR / 'styles/base.mplstyle',
1618
])
1719

18-
1920
np.random.seed(123)
2021

2122
X = np.linspace(0.5, 3.5, 100)
@@ -144,5 +145,5 @@ def text(x, y, text):
144145
connectionstyle="arc3",
145146
color=color))
146147

147-
plt.savefig("../figures/anatomy.pdf")
148+
fig.savefig(ROOT_DIR / "figures/anatomy.pdf")
148149
# plt.show()

Diff for: scripts/annotate.py

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

911

12+
ROOT_DIR = pathlib.Path(__file__).parent.parent
13+
1014
fig = plt.figure(figsize=(6, 1))
1115
# ax = plt.subplot(111, frameon=False, aspect=.1)
1216
# b = 0.0
@@ -23,5 +27,5 @@
2327
plt.text( 5.5, 0.6, "xy\nxycoords", size=10, va="top", ha="center", color=".5")
2428
plt.text( .75, 0.6, "xytext\ntextcoords", size=10, va="top", ha="center", color=".5")
2529

26-
plt.savefig("../figures/annotate.pdf")
30+
fig.savefig(ROOT_DIR / "figures/annotate.pdf")
2731
# plt.show()

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import pathlib
2+
13
import matplotlib.pyplot as plt
24
import matplotlib.patches as mpatches
35

6+
7+
ROOT_DIR = pathlib.Path(__file__).parent.parent
8+
49
styles = mpatches.ArrowStyle.get_styles()
510

611

@@ -29,5 +34,5 @@ def demo_con_style(ax, connectionstyle):
2934
transform=ax.transAxes,
3035
family="Source Code Pro", ha="center", va="top")
3136

32-
plt.savefig("../figures/annotation-arrow-styles.pdf")
37+
fig.savefig(ROOT_DIR / "figures/annotation-arrow-styles.pdf")
3338
# plt.show()

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import matplotlib.pyplot as plt
55

66

7+
ROOT_DIR = pathlib.Path(__file__).parent.parent
8+
79
mpl.style.use([
8-
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
9-
pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle',
10+
ROOT_DIR / 'styles/base.mplstyle',
11+
ROOT_DIR / 'styles/plotlet-grid.mplstyle',
1012
])
1113
mpl.rc('lines', markersize=3)
1214

@@ -41,4 +43,4 @@ def demo_con_style(ax, connectionstyle):
4143
for ax in axs.flat:
4244
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
4345

44-
plt.savefig("../figures/annotation-connection-styles.pdf")
46+
fig.savefig(ROOT_DIR / "figures/annotation-connection-styles.pdf")

Diff for: scripts/basic-plots.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import matplotlib.pyplot as plt
1212

1313

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

1921

@@ -29,7 +31,7 @@
2931
Y = 4 + 2*np.sin(2*X)
3032
ax.plot(X, Y, color="C1")
3133
ax.grid()
32-
fig.savefig("../figures/basic-plot.pdf")
34+
fig.savefig(ROOT_DIR / "figures/basic-plot.pdf")
3335

3436
# Basic line plot (color)
3537
# -----------------------------------------------------------------------------
@@ -38,7 +40,7 @@
3840
Y = 4 + 2*np.sin(2*X)
3941
ax.plot(X, Y, color="black")
4042
ax.grid()
41-
fig.savefig("../figures/basic-plot-color.pdf")
43+
fig.savefig(ROOT_DIR / "figures/basic-plot-color.pdf")
4244

4345
# Basic scatter plot
4446
# -----------------------------------------------------------------------------
@@ -49,7 +51,7 @@
4951
ax.scatter(X, Y, 5, zorder=10,
5052
edgecolor="white", facecolor="C1", linewidth=0.25)
5153
ax.grid()
52-
fig.savefig("../figures/basic-scatter.pdf")
54+
fig.savefig(ROOT_DIR / "figures/basic-scatter.pdf")
5355

5456
# Basic bar plot
5557
# -----------------------------------------------------------------------------
@@ -61,7 +63,7 @@
6163
edgecolor="white", facecolor="C1", linewidth=0.25)
6264
ax.set_axisbelow(True)
6365
ax.grid()
64-
fig.savefig("../figures/basic-bar.pdf")
66+
fig.savefig(ROOT_DIR / "figures/basic-bar.pdf")
6567

6668
# Basic imshow plot
6769
# -----------------------------------------------------------------------------
@@ -72,7 +74,7 @@
7274
Z[..., 3] = np.random.uniform(0.25, 1.0, (8, 8))
7375
ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest")
7476
ax.grid(linewidth=0.25, color="white")
75-
fig.savefig("../figures/basic-imshow.pdf")
77+
fig.savefig(ROOT_DIR / "figures/basic-imshow.pdf")
7678

7779
# Basic pcolormesh plot
7880
# -----------------------------------------------------------------------------
@@ -84,7 +86,7 @@
8486
plt.pcolormesh(X, Y, Z, cmap='Oranges', shading='auto')
8587
ax.set_xlim(-3, 3), ax.set_xticks(np.arange(-3, 4))
8688
ax.set_ylim(-3, 3), ax.set_yticks(np.arange(-3, 4))
87-
fig.savefig("../figures/basic-pcolormesh.pdf")
89+
fig.savefig(ROOT_DIR / "figures/basic-pcolormesh.pdf")
8890

8991
# Basic contour plot
9092
# -----------------------------------------------------------------------------
@@ -95,7 +97,7 @@
9597
plt.contourf(Z, len(colors), extent=[0, 8, 0, 8], colors=colors)
9698
plt.contour(Z, len(colors), extent=[0, 8, 0, 8], colors="white",
9799
linewidths=0.125, nchunk=10)
98-
fig.savefig("../figures/basic-contour.pdf")
100+
fig.savefig(ROOT_DIR / "figures/basic-contour.pdf")
99101

100102
# Basic pie plot
101103
# -----------------------------------------------------------------------------
@@ -110,7 +112,7 @@
110112
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
111113
ax.pie(X, colors=colors, radius=3, center=(4, 4),
112114
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
113-
fig.savefig("../figures/basic-pie.pdf")
115+
fig.savefig(ROOT_DIR / "figures/basic-pie.pdf")
114116

115117
# Basic text plot
116118
# -----------------------------------------------------------------------------
@@ -119,7 +121,7 @@
119121
ax.grid(linewidth=0.25, color="0.75")
120122
ax.text(4, 4, "TEXT", color="C1", size=8, weight="bold",
121123
ha="center", va="center", rotation=25)
122-
fig.savefig("../figures/basic-text.pdf")
124+
fig.savefig(ROOT_DIR / "figures/basic-text.pdf")
123125

124126
# Basic fill plot
125127
# -----------------------------------------------------------------------------
@@ -132,7 +134,7 @@
132134
plt.plot(X, (Y1+Y2)/2, color="C1", linewidth=0.5)
133135
ax.set_axisbelow(True)
134136
ax.grid(color="0.75")
135-
fig.savefig("../figures/basic-fill.pdf")
137+
fig.savefig(ROOT_DIR / "figures/basic-fill.pdf")
136138

137139
# Basic quiver plot
138140
# -----------------------------------------------------------------------------
@@ -145,4 +147,4 @@
145147
angles='xy', scale_units='xy', scale=0.5, width=.05)
146148
ax.set_axisbelow(True)
147149
ax.grid(color="0.75")
148-
fig.savefig("../figures/basic-quiver.pdf")
150+
fig.savefig(ROOT_DIR / "figures/basic-quiver.pdf")

Diff for: scripts/colorbar.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
6+
import pathlib
7+
58
import numpy as np
69
import matplotlib as mpl
710
import matplotlib.pyplot as plt
811

912

13+
ROOT_DIR = pathlib.Path(__file__).parent.parent
14+
1015
fig = plt.figure(figsize=(6, .65))
1116
# ax = plt.subplot(111, frameon=False, aspect=.1)
1217
b = 0.025
@@ -19,5 +24,5 @@
1924
plt.colorbar(sm, cax=ax, ticks=np.linspace(0, 1, 11),
2025
orientation="horizontal")
2126

22-
plt.savefig("../figures/colorbar.pdf")
27+
fig.savefig(ROOT_DIR / "figures/colorbar.pdf")
2328
# plt.show()

Diff for: scripts/colormaps.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import pathlib
2+
13
import numpy as np
24
import matplotlib.pyplot as plt
35

46

7+
ROOT_DIR = pathlib.Path(__file__).parent.parent
8+
59
figsize = 4.0, 0.25
610
fig = plt.figure(figsize=figsize)
711
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1)
@@ -62,5 +66,5 @@
6266
family = "Source Pro Serif", size=10, ha="center", va="center")
6367
"""
6468

65-
plt.savefig("../figures/colormap-%s.pdf" % cmap)
69+
fig.savefig(ROOT_DIR / f"figures/colormap-{cmap}.pdf")
6670
ax.clear()

Diff for: scripts/colornames.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import matplotlib.pyplot as plt
1212

1313

14+
ROOT_DIR = pathlib.Path(__file__).parent.parent
15+
1416
mpl.style.use([
15-
pathlib.Path(__file__).parent/'../styles/base.mplstyle',
17+
ROOT_DIR / 'styles/base.mplstyle',
1618
])
1719
mpl.rc('figure.constrained_layout', h_pad=0, w_pad=0, hspace=0, wspace=0)
1820

@@ -54,4 +56,4 @@
5456
ax.set_ylim(0, Y)
5557
ax.set_axis_off()
5658

57-
plt.savefig("../figures/colornames.pdf")
59+
fig.savefig(ROOT_DIR / "figures/colornames.pdf")

Diff for: scripts/colors.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
# Matplotlib cheat sheet
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
5+
6+
import pathlib
7+
58
import numpy as np
69
import matplotlib as mpl
710
import matplotlib.pyplot as plt
811

912

13+
ROOT_DIR = pathlib.Path(__file__).parent.parent
14+
1015
figsize = 4.0, 0.25
1116
fig = plt.figure(figsize=figsize)
1217
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1)
@@ -36,5 +41,5 @@
3641
ax.text((i+0.5)*dx, (ymin+ymax)/2, text, color=color, zorder=10,
3742
family="Source Code Pro", size=9, ha="center", va="center")
3843

39-
plt.savefig("../figures/colors-%s.pdf" % name)
44+
fig.savefig(ROOT_DIR / f"figures/colors-{name}.pdf")
4045
ax.clear()

0 commit comments

Comments
 (0)