Skip to content

Commit 6a080d5

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 3d7d3ff commit 6a080d5

37 files changed

+259
-87
lines changed

scripts/adjustements.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
# Released under the BSD License
44
# -----------------------------------------------------------------------------
55

6+
from pathlib import Path
7+
68
import numpy as np
79
import matplotlib.pyplot as plt
810
import matplotlib.patches as mpatches
911
from matplotlib.collections import PatchCollection
1012

1113

14+
REPO = Path(__file__).parent.parent
15+
1216
fig = plt.figure(figsize=(4.25, 4.25 * 95/115))
1317
ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1,
1418
xlim=(0-5, 100+10), ylim=(-10, 80+5), xticks=[], yticks=[])
@@ -125,5 +129,5 @@ def int_arrow(p0, p1):
125129
rotation="vertical", ha="center", va="center", size="x-small")
126130

127131

128-
plt.savefig("../figures/adjustments.pdf")
132+
fig.savefig(REPO / "figures/adjustments.pdf")
129133
# plt.show()

scripts/advanced-plots.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
# -----------------------------------------------------------------------------
55

66
# Scripts to generate all the basic plots
7+
from pathlib import Path
8+
79
import numpy as np
810
import matplotlib as mpl
911
import matplotlib.pyplot as plt
1012

13+
14+
REPO = Path(__file__).parent.parent
15+
1116
fig = plt.figure(figsize=(0.4, 0.4))
1217
mpl.rcParams['axes.linewidth'] = 0.5
1318
mpl.rcParams['xtick.major.size'] = 0.0
@@ -23,7 +28,7 @@
2328
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
2429
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
2530
ax.grid(linewidth=0.125)
26-
plt.savefig("../figures/advanced-step.pdf")
31+
fig.savefig(REPO / "figures/advanced-step.pdf")
2732
ax.clear()
2833

2934
# Violin plot
@@ -39,7 +44,7 @@
3944
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
4045
ax.set_axisbelow(True)
4146
ax.grid(linewidth=0.125)
42-
plt.savefig("../figures/advanced-violin.pdf")
47+
fig.savefig(REPO / "figures/advanced-violin.pdf")
4348
ax.clear()
4449

4550
# Boxplot
@@ -61,7 +66,7 @@
6166
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
6267
ax.set_axisbelow(True)
6368
ax.grid(linewidth=0.125)
64-
plt.savefig("../figures/advanced-boxplot.pdf")
69+
fig.savefig(REPO / "figures/advanced-boxplot.pdf")
6570
ax.clear()
6671

6772
# Barbs plot
@@ -76,7 +81,7 @@
7681
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
7782
ax.set_axisbelow(True)
7883
ax.grid(linewidth=0.125)
79-
plt.savefig("../figures/advanced-barbs.pdf")
84+
fig.savefig(REPO / "figures/advanced-barbs.pdf")
8085
ax.clear()
8186

8287
# Event plot
@@ -90,7 +95,7 @@
9095
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
9196
ax.set_axisbelow(True)
9297
ax.grid(linewidth=0.125)
93-
plt.savefig("../figures/advanced-event.pdf")
98+
fig.savefig(REPO / "figures/advanced-event.pdf")
9499
ax.clear()
95100

96101
# Errorbar plot
@@ -104,7 +109,7 @@
104109
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
105110
ax.set_axisbelow(True)
106111
ax.grid(linewidth=0.125)
107-
plt.savefig("../figures/advanced-errorbar.pdf")
112+
fig.savefig(REPO / "figures/advanced-errorbar.pdf")
108113
ax.clear()
109114

110115
# Hexbin plot
@@ -119,7 +124,7 @@
119124
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
120125
ax.set_axisbelow(True)
121126
ax.grid(linewidth=0.125)
122-
plt.savefig("../figures/advanced-hexbin.pdf")
127+
fig.savefig(REPO / "figures/advanced-hexbin.pdf")
123128
ax.clear()
124129

125130
# Hist plot
@@ -131,7 +136,7 @@
131136
ax.set_ylim(0, 80), ax.set_yticks(np.arange(1, 80, 10))
132137
ax.set_axisbelow(True)
133138
ax.grid(linewidth=0.125)
134-
plt.savefig("../figures/advanced-hist.pdf")
139+
fig.savefig(REPO / "figures/advanced-hist.pdf")
135140
ax.clear()
136141

137142
# Xcorr plot
@@ -145,5 +150,5 @@
145150
ax.set_ylim(-.25, .25), ax.set_yticks(np.linspace(-.25, .25, 9))
146151
ax.set_axisbelow(True)
147152
ax.grid(linewidth=0.125)
148-
plt.savefig("../figures/advanced-xcorr.pdf")
153+
fig.savefig(REPO / "figures/advanced-xcorr.pdf")
149154
ax.clear()

scripts/anatomy.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
# Author: Nicolas P. Rougier
44
# License: BSD
55
# ----------------------------------------------------------------------------
6+
7+
from pathlib import Path
8+
69
import numpy as np
710
import matplotlib.pyplot as plt
811
from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter
912

13+
14+
REPO = Path(__file__).parent.parent
15+
1016
np.random.seed(123)
1117

1218
X = np.linspace(0.5, 3.5, 100)
@@ -136,5 +142,5 @@ def text(x, y, text):
136142
connectionstyle="arc3",
137143
color=color))
138144

139-
plt.savefig("../figures/anatomy.pdf")
145+
fig.savefig(REPO / "figures/anatomy.pdf")
140146
# plt.show()

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+
from pathlib import Path
6+
57
import numpy as np
68
import matplotlib as mpl
79
import matplotlib.pyplot as plt
810

911

12+
REPO = 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(REPO / "figures/annotate.pdf")
2731
# plt.show()

scripts/annotation-arrow-styles.py

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

6+
7+
REPO = 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(REPO / "figures/annotation-arrow-styles.pdf")
3338
# plt.show()

scripts/annotation-connection-styles.py

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

35

6+
REPO = Path(__file__).parent.parent
7+
8+
49
def demo_con_style(ax, connectionstyle):
510
x1, y1 = 0.3, 0.2
611
x2, y2 = 0.8, 0.6
@@ -33,5 +38,5 @@ def demo_con_style(ax, connectionstyle):
3338
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)
3439
fig.tight_layout(pad=0.2)
3540

36-
plt.savefig("../figures/annotation-connection-styles.pdf")
41+
fig.savefig(REPO / "figures/annotation-connection-styles.pdf")
3742
# plt.show()

scripts/basic-plots.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
# -----------------------------------------------------------------------------
55

66
# Scripts to generate all the basic plots
7+
from pathlib import Path
8+
79
import numpy as np
810
import matplotlib as mpl
911
import matplotlib.pyplot as plt
1012

13+
14+
REPO = Path(__file__).parent.parent
15+
1116
fig = plt.figure(figsize=(0.4, 0.4))
1217
mpl.rcParams['axes.linewidth'] = 0.5
1318
mpl.rcParams['xtick.major.size'] = 0.0
@@ -23,7 +28,7 @@
2328
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
2429
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
2530
ax.grid(linewidth=0.125)
26-
plt.savefig("../figures/basic-plot.pdf")
31+
fig.savefig(REPO / "figures/basic-plot.pdf")
2732
ax.clear()
2833

2934
# Basic line plot (color)blaPwd
@@ -35,7 +40,7 @@
3540
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
3641
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
3742
ax.grid(linewidth=0.125)
38-
plt.savefig("../figures/basic-plot-color.pdf")
43+
fig.savefig(REPO / "figures/basic-plot-color.pdf")
3944
ax.clear()
4045

4146
# Basic scatter plot
@@ -48,7 +53,7 @@
4853
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
4954
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
5055
ax.grid(linewidth=0.125)
51-
plt.savefig("../figures/basic-scatter.pdf")
56+
fig.savefig(REPO / "figures/basic-scatter.pdf")
5257
ax.clear()
5358

5459
# Basic bar plot
@@ -62,7 +67,7 @@
6267
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
6368
ax.set_axisbelow(True)
6469
ax.grid(linewidth=0.125)
65-
plt.savefig("../figures/basic-bar.pdf")
70+
fig.savefig(REPO / "figures/basic-bar.pdf")
6671
ax.clear()
6772

6873
# Basic imshow plot
@@ -75,7 +80,7 @@
7580
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
7681
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
7782
ax.grid(linewidth=0.25, color="white")
78-
plt.savefig("../figures/basic-imshow.pdf")
83+
fig.savefig(REPO / "figures/basic-imshow.pdf")
7984
ax.clear()
8085

8186
# Basic pcolormesh plot
@@ -87,7 +92,7 @@
8792
plt.pcolormesh(X, Y, Z, cmap='Oranges', shading='auto')
8893
ax.set_xlim(-3, 3), ax.set_xticks(np.arange(-3, 4))
8994
ax.set_ylim(-3, 3), ax.set_yticks(np.arange(-3, 4))
90-
plt.savefig("../figures/basic-pcolormesh.pdf")
95+
fig.savefig(REPO / "figures/basic-pcolormesh.pdf")
9196
ax.clear()
9297

9398
# Basic contour plot
@@ -100,7 +105,7 @@
100105
linewidths=0.125, nchunk=10)
101106
ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8))
102107
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
103-
plt.savefig("../figures/basic-contour.pdf")
108+
fig.savefig(REPO / "figures/basic-contour.pdf")
104109
ax.clear()
105110

106111
# Basic pie plot
@@ -117,7 +122,7 @@
117122
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
118123
ax.pie(X, colors=colors, radius=3, center=(4, 4),
119124
wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True)
120-
plt.savefig("../figures/basic-pie.pdf")
125+
fig.savefig(REPO / "figures/basic-pie.pdf")
121126
ax.clear()
122127

123128
# Basic text plot
@@ -128,7 +133,7 @@
128133
ax.grid(linewidth=0.25, color="0.75")
129134
ax.text(4, 4, "TEXT", color="C1", size=8, weight="bold",
130135
ha="center", va="center", rotation=25)
131-
plt.savefig("../figures/basic-text.pdf")
136+
fig.savefig(REPO / "figures/basic-text.pdf")
132137
ax.clear()
133138

134139
# Basic fill plot
@@ -143,7 +148,7 @@
143148
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
144149
ax.set_axisbelow(True)
145150
ax.grid(linewidth=0.125, color="0.75")
146-
plt.savefig("../figures/basic-fill.pdf")
151+
fig.savefig(REPO / "figures/basic-fill.pdf")
147152
ax.clear()
148153

149154
# Basic quiver plot
@@ -158,5 +163,5 @@
158163
ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8))
159164
ax.set_axisbelow(True)
160165
ax.grid(linewidth=0.125, color="0.75")
161-
plt.savefig("../figures/basic-quiver.pdf")
166+
fig.savefig(REPO / "figures/basic-quiver.pdf")
162167
ax.clear()

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+
from pathlib import Path
7+
58
import numpy as np
69
import matplotlib as mpl
710
import matplotlib.pyplot as plt
811

912

13+
REPO = 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(REPO / "figures/colorbar.pdf")
2328
# plt.show()

scripts/colormaps.py

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

46

7+
REPO = 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(REPO / f"figures/colormap-{cmap}.pdf")
6670
ax.clear()

scripts/colornames.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
66
Simple plot example with the named colors and its visual representation.
77
"""
8-
from __future__ import division
8+
9+
from pathlib import Path
910

1011
import matplotlib.pyplot as plt
1112
from matplotlib import colors as mcolors
1213

1314

15+
REPO = Path(__file__).parent.parent
16+
1417
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
1518

1619
# Sort colors by hue, saturation, value and name.
@@ -53,5 +56,5 @@
5356
top=1, bottom=0,
5457
hspace=0, wspace=0)
5558

56-
plt.savefig("../figures/colornames.pdf")
59+
fig.savefig(REPO / "figures/colornames.pdf")
5760
# plt.show()

0 commit comments

Comments
 (0)