Skip to content

Commit fa4cd5f

Browse files
committed
Refactor plotting functions: add title helper, update labels for clarity, and adjust figure layout
1 parent e707f9b commit fa4cd5f

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

docs/paper/figure1.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,21 @@ def add_waypoint_markers(ax, distances, offset=15, marker_size=70):
4747
)
4848

4949

50+
def add_title(ax, title, fontsize=13, y0=1.03):
51+
"""Add title."""
52+
ax.text(
53+
0,
54+
y0,
55+
title,
56+
ha="left",
57+
va="bottom",
58+
transform=ax.transAxes,
59+
fontsize=fontsize,
60+
)
61+
62+
5063
# fig
51-
fig = plt.figure(figsize=(12, 13), dpi=300)
64+
fig = plt.figure(figsize=(9, 13), dpi=300)
5265

5366
# custom layout
5467
gs = gridspec.GridSpec(3, 2, height_ratios=[1.5, 1, 1])
@@ -59,20 +72,20 @@ def add_waypoint_markers(ax, distances, offset=15, marker_size=70):
5972
ax4 = fig.add_subplot(gs[2, 1])
6073

6174
# overview image
62-
ax0.set_title(r"$\bf{a}$" + ") MFP expedition overview", loc="left")
75+
add_title(ax0, r"$\bf{a}$" + ") MFP expedition overview", y0=1.01)
6376
img = mpimg.imread(f"{SAMPLE_DIR}expedition_overview.png")
6477
ax0.imshow(img)
6578
ax0.axis("off")
6679

6780
# adcp
68-
ax1.set_title(r"$\bf{b}$" + ") ADCP (flow speed)", loc="left")
81+
add_title(ax1, r"$\bf{b}$" + ") ADCP (flow speed)")
6982
ax1.set_ylabel("Depth (m)")
7083
ax1.set_xlabel("Distance (km)")
7184
plot_adcp(adcp_ds, ax1)
7285
add_waypoint_markers(ax1, waypoint_distances)
7386

7487
# drifters
75-
ax2.set_title(r"$\bf{c}$" + ") Surface drifters", loc="left")
88+
add_title(ax2, r"$\bf{c}$" + ") Surface drifters")
7689
plot_drifters(
7790
drifter_ds,
7891
ax2,
@@ -81,7 +94,7 @@ def add_waypoint_markers(ax, distances, offset=15, marker_size=70):
8194
)
8295

8396
# CTD (temperature)
84-
ax3.set_title(r"$\bf{d}$" + ") CTD (temperature)", loc="left")
97+
add_title(ax3, r"$\bf{d}$" + ") CTD (temperature)")
8598
ax3.set_ylabel("Depth (m)")
8699
ax3.set_xlabel("Distance (km)")
87100
_, _distances_regular, _var_masked = plot_ctd(
@@ -96,7 +109,7 @@ def add_waypoint_markers(ax, distances, offset=15, marker_size=70):
96109
add_waypoint_markers(ax3, ctd_wp_distances, offset=45, marker_size=60)
97110

98111
# CTD (oxygen)
99-
ax4.set_title(r"$\bf{e}$" + ") CTD (oxygen)", loc="left")
112+
add_title(ax4, r"$\bf{e}$" + ") CTD (oxygen)")
100113
ax4.set_xlabel("Distance (km)")
101114
plot_ctd(
102115
ctd_bgc_ds, ax4, plot_variable="oxygen", vmin=0, vmax=ctd_bgc_ds.o2.max()
@@ -107,6 +120,6 @@ def add_waypoint_markers(ax, distances, offset=15, marker_size=70):
107120
plt.tight_layout()
108121
plt.show()
109122

110-
fig.savefig("figure1.png", dpi=300)
123+
fig.savefig("figure1.png", dpi=300, bbox_inches="tight")
111124

112125
# %%

docs/paper/plotting_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def plot_drifters(drifter_ds, ax, vmin, vmax, PLOT_VARIABLE="temperature"):
5252
edgecolor="black",
5353
zorder=4,
5454
transform=PROJ,
55-
label="Waypoint/release location"
55+
label="Waypoint"
5656
if i == 0
5757
else None, # only label first for legend
5858
)
@@ -204,10 +204,10 @@ def plot_adcp(ds, ax, axes_labels=False):
204204
# =====================================================
205205

206206

207-
def _add_cmap(da, cmap, ax, label, vmin, vmax, orientation="horizontal", shrink=0.90):
207+
def _add_cmap(da, cmap, ax, label, vmin, vmax, orientation="horizontal", shrink=0.90, fraction=0.05, pad=0.2):
208208
sm = plt.cm.ScalarMappable(cmap=cmap, norm=mcolors.Normalize(vmin, vmax))
209209
sm._A = []
210-
plt.colorbar(sm, ax=ax, orientation=orientation, label=label, shrink=shrink)
210+
plt.colorbar(sm, ax=ax, orientation=orientation, label=label, shrink=shrink, fraction=fraction, pad=pad)
211211

212212

213213
def _haversine(lon1, lat1, lon2, lat2):

0 commit comments

Comments
 (0)