diff --git a/autoarray/config/visualize/general.yaml b/autoarray/config/visualize/general.yaml index 0ba855dd..1890a5d0 100644 --- a/autoarray/config/visualize/general.yaml +++ b/autoarray/config/visualize/general.yaml @@ -21,6 +21,7 @@ colormap: autoarray # Default colormap for 2D plots. Use any matpl ticks: extent_factor_2d: 0.75 # Fraction of half-extent used for 2D tick positions (< 1.0 pulls ticks inward from edges). number_of_ticks_2d: 3 # Number of ticks on each spatial axis of 2D plots. + symbol_over_decimal: false # If true, place the arcsec double-prime symbol over the decimal point, e.g. 3.″8, instead of after the value, e.g. 3.8". contour: total_contours: 10 # Number of contour levels drawn over log10 (and explicit linear) plots. include_values: true # Whether to label each contour line with its value. diff --git a/autoarray/plot/utils.py b/autoarray/plot/utils.py index bdae788f..0ed80f80 100644 --- a/autoarray/plot/utils.py +++ b/autoarray/plot/utils.py @@ -864,6 +864,14 @@ def _conf_ticks(key: str, default: float) -> float: return default +def _conf_ticks_flag(key: str, default: bool) -> bool: + try: + from autoconf import conf + return bool(conf.instance["visualize"]["general"]["ticks"][key]) + except Exception: + return default + + def _inward_ticks(lo: float, hi: float, factor: float, n: int) -> np.ndarray: """Return *n* tick positions pulled inward from the extent edges by *factor*.""" centre = (lo + hi) / 2.0 @@ -895,11 +903,24 @@ def _arcsec_labels(ticks) -> List[str]: """Format tick values as arcsecond coordinate strings. Values that all end in ``.0`` are stripped of the decimal point before the - ``"`` suffix is appended, so ``[-1.0, 0.0, 1.0]`` → ``['-1"', '0"', '1"']``. + ``"`` suffix is appended, so ``[-1.0, 0.0, 1.0]`` becomes + ``['-1"', '0"', '1"']``. By default decimal labels keep the suffix form + ``3.8"``. When ``ticks.symbol_over_decimal`` is true, labels use the + double-prime arcsecond symbol and decimal labels place it over the decimal + point, e.g. ``3.″8``. """ labels = [f'{v:g}' for v in ticks] if all(label.endswith(".0") for label in labels): labels = [label[:-2] for label in labels] + if _conf_ticks_flag("symbol_over_decimal", False): + symbol_labels = [] + for label in labels: + if "." in label: + int_part, frac_part = label.split(".", 1) + symbol_labels.append(f"{int_part}.″{frac_part}") + else: + symbol_labels.append(f"{label}″") + return symbol_labels return [f'{label}"' for label in labels] diff --git a/test_autoarray/plot/test_utils.py b/test_autoarray/plot/test_utils.py new file mode 100644 index 00000000..f8b2defa --- /dev/null +++ b/test_autoarray/plot/test_utils.py @@ -0,0 +1,26 @@ +from autoconf import conf + +from autoarray.plot.utils import _arcsec_labels + + +def test_arcsec_labels_default_suffix_format(): + conf.instance["visualize"]["general"]["ticks"]["symbol_over_decimal"] = False + + assert _arcsec_labels([-1.0, 0.0, 1.0]) == ['-1"', '0"', '1"'] + assert _arcsec_labels([3.8]) == ['3.8"'] + + +def test_arcsec_labels_symbol_over_decimal(): + ticks = conf.instance["visualize"]["general"]["ticks"] + original = ticks.get("symbol_over_decimal", False) + try: + ticks["symbol_over_decimal"] = True + + assert _arcsec_labels([-1.69, 0.13, 1.95]) == [ + "-1.″69", + "0.″13", + "1.″95", + ] + assert _arcsec_labels([3.0]) == ["3″"] + finally: + ticks["symbol_over_decimal"] = original