Skip to content

Commit 6f48e13

Browse files
committed
Wire mat_plot font sizes and figsize from visualize/general.yaml
Previously the plot functions used hardcoded font sizes (title=16, xlabel/ylabel=14, ticks=12) and conf_figsize read a non-existent path, so config values were never applied. - utils.py: add conf_mat_plot_fontsize() to read from visualize/general/mat_plot/<section>/fontsize - utils.py: add apply_labels() which calls set_title/set_xlabel/ set_ylabel/tick_params using config-driven font sizes; eliminates the duplicated 4-line label block in every plot function - utils.py: fix conf_figsize() to read from mat_plot/figure/figsize (the key that actually exists in the config); add _parse_figsize() helper to handle YAML tuple-as-string encoding "(7, 7)" - array.py, grid.py, yx.py, inversion.py: replace hardcoded label blocks with apply_labels() - __init__.py: export apply_labels and conf_mat_plot_fontsize https://claude.ai/code/session_01B9sVEV54XWCa2LJw1C8gvv
1 parent 67fd418 commit 6f48e13

6 files changed

Lines changed: 96 additions & 20 deletions

File tree

autoarray/plot/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def _set_backend():
2626
from autoarray.plot.inversion import plot_inversion_reconstruction
2727
from autoarray.plot.utils import (
2828
apply_extent,
29+
apply_labels,
2930
conf_figsize,
31+
conf_mat_plot_fontsize,
3032
save_figure,
3133
subplot_save,
3234
auto_mask_edge,

autoarray/plot/array.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from autoarray.plot.utils import (
1313
apply_extent,
14+
apply_labels,
1415
conf_figsize,
1516
save_figure,
1617
zoom_array,
@@ -262,10 +263,7 @@ def plot_array(
262263
pass
263264

264265
# --- labels / ticks --------------------------------------------------------
265-
ax.set_title(title, fontsize=16)
266-
ax.set_xlabel(xlabel, fontsize=14)
267-
ax.set_ylabel(ylabel, fontsize=14)
268-
ax.tick_params(labelsize=12)
266+
apply_labels(ax, title=title, xlabel=xlabel, ylabel=ylabel)
269267

270268
if extent is not None:
271269
apply_extent(ax, extent)

autoarray/plot/grid.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import matplotlib.pyplot as plt
1010
import numpy as np
1111

12-
from autoarray.plot.utils import apply_extent, conf_figsize, save_figure, numpy_lines
12+
from autoarray.plot.utils import (
13+
apply_extent,
14+
apply_labels,
15+
conf_figsize,
16+
save_figure,
17+
numpy_lines,
18+
)
1319

1420

1521
def plot_grid(
@@ -142,10 +148,7 @@ def plot_grid(
142148
ax.plot(line[:, 1], line[:, 0], linewidth=2)
143149

144150
# --- labels ----------------------------------------------------------------
145-
ax.set_title(title, fontsize=16)
146-
ax.set_xlabel(xlabel, fontsize=14)
147-
ax.set_ylabel(ylabel, fontsize=14)
148-
ax.tick_params(labelsize=12)
151+
apply_labels(ax, title=title, xlabel=xlabel, ylabel=ylabel)
149152

150153
# --- extent ----------------------------------------------------------------
151154
if extent is None:

autoarray/plot/inversion.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import numpy as np
1111
from matplotlib.colors import LogNorm, Normalize
1212

13-
from autoarray.plot.utils import apply_extent, conf_figsize, save_figure
13+
from autoarray.plot.utils import apply_extent, apply_labels, conf_figsize, save_figure
1414

1515

1616
def plot_inversion_reconstruction(
@@ -125,10 +125,7 @@ def plot_inversion_reconstruction(
125125

126126
apply_extent(ax, extent)
127127

128-
ax.set_title(title, fontsize=16)
129-
ax.set_xlabel(xlabel, fontsize=14)
130-
ax.set_ylabel(ylabel, fontsize=14)
131-
ax.tick_params(labelsize=12)
128+
apply_labels(ax, title=title, xlabel=xlabel, ylabel=ylabel)
132129

133130
if owns_figure:
134131
save_figure(

autoarray/plot/utils.py

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,102 @@ def subplot_save(fig, output_path, output_filename, output_format):
305305
plt.close(fig)
306306

307307

308+
def conf_mat_plot_fontsize(section: str, default: int) -> int:
309+
"""Read a font size from the ``mat_plot`` section of ``visualize/general.yaml``.
310+
311+
Parameters
312+
----------
313+
section
314+
Sub-key inside ``mat_plot``, e.g. ``"title"``, ``"xlabel"``,
315+
``"ylabel"``, ``"xticks"``, or ``"yticks"``.
316+
default
317+
Value returned when the config key is absent or unreadable.
318+
319+
Returns
320+
-------
321+
int
322+
The configured font size.
323+
"""
324+
try:
325+
from autoconf import conf
326+
327+
return int(
328+
conf.instance["visualize"]["general"]["mat_plot"][section]["fontsize"]
329+
)
330+
except Exception:
331+
return default
332+
333+
334+
def _parse_figsize(raw) -> Tuple[int, int]:
335+
"""Convert *raw* (a tuple/list or a string like ``"(7, 7)"``) to a 2-tuple."""
336+
if isinstance(raw, (tuple, list)):
337+
return tuple(raw)
338+
import ast
339+
340+
return tuple(ast.literal_eval(str(raw)))
341+
342+
308343
def conf_figsize(context: str = "figures") -> Tuple[int, int]:
309344
"""
310345
Read figsize from ``visualize/general.yaml`` for the given context.
311346
347+
For single-panel figures the value is taken from
348+
``mat_plot/figure/figsize``; the *context* argument is kept for
349+
backward compatibility with subplot callers that pass ``"subplots"``.
350+
312351
Parameters
313352
----------
314353
context
315-
Either ``"figures"`` (single-panel) or ``"subplots"`` (multi-panel).
354+
``"figures"`` (single-panel) or ``"subplots"`` (multi-panel).
316355
"""
317356
try:
318357
from autoconf import conf
319358

359+
if context == "figures":
360+
raw = conf.instance["visualize"]["general"]["mat_plot"]["figure"]["figsize"]
361+
return _parse_figsize(raw)
320362
return tuple(conf.instance["visualize"]["general"][context]["figsize"])
321363
except Exception:
322364
return (7, 7) if context == "figures" else (19, 16)
323365

324366

367+
def apply_labels(
368+
ax: plt.Axes,
369+
title: str = "",
370+
xlabel: str = "",
371+
ylabel: str = "",
372+
) -> None:
373+
"""Apply title, axis labels, and tick font sizes to *ax* from config.
374+
375+
Reads font sizes from the ``mat_plot`` section of
376+
``visualize/general.yaml`` so that users can override them globally
377+
without touching call sites. Falls back to the values that the
378+
old ``MatWrap`` system used when the config is unavailable.
379+
380+
Parameters
381+
----------
382+
ax
383+
The matplotlib axes to configure.
384+
title
385+
Title string.
386+
xlabel
387+
X-axis label string.
388+
ylabel
389+
Y-axis label string.
390+
"""
391+
title_fs = conf_mat_plot_fontsize("title", default=16)
392+
xlabel_fs = conf_mat_plot_fontsize("xlabel", default=14)
393+
ylabel_fs = conf_mat_plot_fontsize("ylabel", default=14)
394+
xticks_fs = conf_mat_plot_fontsize("xticks", default=12)
395+
yticks_fs = conf_mat_plot_fontsize("yticks", default=12)
396+
397+
ax.set_title(title, fontsize=title_fs)
398+
ax.set_xlabel(xlabel, fontsize=xlabel_fs)
399+
ax.set_ylabel(ylabel, fontsize=ylabel_fs)
400+
ax.tick_params(axis="x", labelsize=xticks_fs)
401+
ax.tick_params(axis="y", labelsize=yticks_fs)
402+
403+
325404
def save_figure(
326405
fig: plt.Figure,
327406
path: str,

autoarray/plot/yx.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import matplotlib.pyplot as plt
1010
import numpy as np
1111

12-
from autoarray.plot.utils import conf_figsize, save_figure
12+
from autoarray.plot.utils import apply_labels, conf_figsize, save_figure
1313

1414

1515
def plot_yx(
@@ -129,10 +129,7 @@ def plot_yx(
129129
ax.fill_between(x, y1, y2, alpha=0.3)
130130

131131
# --- labels ----------------------------------------------------------------
132-
ax.set_title(title, fontsize=16)
133-
ax.set_xlabel(xlabel, fontsize=14)
134-
ax.set_ylabel(ylabel, fontsize=14)
135-
ax.tick_params(labelsize=12)
132+
apply_labels(ax, title=title, xlabel=xlabel, ylabel=ylabel)
136133

137134
if label is not None:
138135
ax.legend(fontsize=12)

0 commit comments

Comments
 (0)