From 83006847dcd592a1620101aee7ac8d009b3174e0 Mon Sep 17 00:00:00 2001 From: Marco Berzborn Date: Fri, 23 Apr 2021 17:15:16 +0200 Subject: [PATCH 1/2] start implementing sh tickers --- spharpy/plot/ticker.py | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 spharpy/plot/ticker.py diff --git a/spharpy/plot/ticker.py b/spharpy/plot/ticker.py new file mode 100644 index 00000000..77dd8bd4 --- /dev/null +++ b/spharpy/plot/ticker.py @@ -0,0 +1,55 @@ +from matplotlib.ticker import Locator, Formatter +import spharpy +import numpy as np + + +class SphericalHarmonicLocator(Locator): + + def __init__(self, offset=1): + self._offset = offset + super().__init__() + + def __call__(self): + dmin, dmax = self.axis.get_data_interval() + + return self.tick_values(dmin, dmax) + + def tick_values(self, vmin, vmax): + n_min, m = spharpy.spherical.acn2nm(vmin) + n_max, m = spharpy.spherical.acn2nm(vmax) + + orders = np.arange(n_min, n_max) + nn_ticks = spharpy.spherical.nm2acn(orders, orders) + self._offset + + return nn_ticks + + +class SphericalHarmonicFormatter(Formatter): + """Formatter for ticks representing axes with spherical harmonics. + + Parameters + ---------- + format : str + The format used, can be either 'n', 'n0', 'acn', or 'nm' + """ + + def __init__( + self, + offset=0, + format='n', + **kwargs): + + super().__init__(**kwargs) + self.format = format + self._offset = offset + + def __call__(self, x, pos=None): + if self.format == 'n': + string = (spharpy.spherical.acn2nm(x)[0]) + self._offset + elif self.format == 'nm': + string = x + elif self.format == 'n0': + n, m = spharpy.spherical.acn2nm(x) + string = f'({n},{m})' + + return string From 1265cb6653725030833517f01d559a32c76d7d93 Mon Sep 17 00:00:00 2001 From: Marco Berzborn Date: Tue, 11 Jul 2023 14:57:16 +0200 Subject: [PATCH 2/2] rename nm to acn --- spharpy/plot/ticker.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/spharpy/plot/ticker.py b/spharpy/plot/ticker.py index 77dd8bd4..f47473db 100644 --- a/spharpy/plot/ticker.py +++ b/spharpy/plot/ticker.py @@ -5,8 +5,10 @@ class SphericalHarmonicLocator(Locator): - def __init__(self, offset=1): + def __init__(self, format='n', offset=0, step=1): self._offset = offset + self._format = format + self._step = step super().__init__() def __call__(self): @@ -15,29 +17,28 @@ def __call__(self): return self.tick_values(dmin, dmax) def tick_values(self, vmin, vmax): - n_min, m = spharpy.spherical.acn2nm(vmin) - n_max, m = spharpy.spherical.acn2nm(vmax) + n_min, m_min = spharpy.spherical.acn2nm(vmin) + n_max, m_max = spharpy.spherical.acn2nm(vmax) - orders = np.arange(n_min, n_max) - nn_ticks = spharpy.spherical.nm2acn(orders, orders) + self._offset + orders = np.arange(n_min, n_max+1, step=self._step) - return nn_ticks + if self._format == 'n': + return spharpy.spherical.nm2acn(orders, orders) + self._offset + elif self._format == 'n0': + return spharpy.spherical.nm2acn( + orders, np.zeros_like(orders)) + self._offset + elif self._format == 'acn': + return np.arange(vmin, vmax) + self._offset class SphericalHarmonicFormatter(Formatter): """Formatter for ticks representing axes with spherical harmonics. - Parameters ---------- format : str - The format used, can be either 'n', 'n0', 'acn', or 'nm' + The format used, can be either 'n', 'acn', or 'nm' """ - - def __init__( - self, - offset=0, - format='n', - **kwargs): + def __init__(self, format='n', offset=0, **kwargs): super().__init__(**kwargs) self.format = format @@ -45,11 +46,13 @@ def __init__( def __call__(self, x, pos=None): if self.format == 'n': - string = (spharpy.spherical.acn2nm(x)[0]) + self._offset - elif self.format == 'nm': + string = spharpy.spherical.acn2nm(x)[0] + self._offset + elif self.format == 'acn': string = x - elif self.format == 'n0': + elif self.format == 'nm': n, m = spharpy.spherical.acn2nm(x) string = f'({n},{m})' + else: + raise ValueError(f'Unknown format {self.format}') return string