From f073f2e682f38453dbe4813e91b5bc519d748ef1 Mon Sep 17 00:00:00 2001 From: Erik Tollerud Date: Fri, 10 Apr 2020 16:52:11 -0400 Subject: [PATCH 1/5] added first cut of plot_quick --- specutils/spectra/spectrum1d.py | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/specutils/spectra/spectrum1d.py b/specutils/spectra/spectrum1d.py index 24def21f3..b5ccf714e 100644 --- a/specutils/spectra/spectrum1d.py +++ b/specutils/spectra/spectrum1d.py @@ -768,3 +768,70 @@ def __repr__(self): result = "".format(inner_str) return result + + + def plot_quick(self, ax=None, x_name='spectral axis', y_name='flux', + **kwargs): + """ + Visualize this spectrum using matplotlib in "histogram style". + + Parameters + ---------- + ax : `matplotlib.axes.Axes` or None + The axis to plot this figure into. If None, use the current + ``pyplot`` axes (which will create a new figure if none exists). + x_name : str or None + The name to use for the x axis (units will be automatically added) + or None to not set the x axis label. + y_name : str or None + The name to use for the y axis (units will be automatically added) + or None to not set the y axis label. + + kwargs are passed into `~matplotlib.axes.Axes.plot`, except for + ``drawstyle`` or ``ds``. + + Returns + ------- + ax : `matplotlib.axes.Axes` + Either ``ax``, or the newly created axes object (if the ``ax`` + parameter is None). + """ + # import is intentionally inside the method to make matplotlib an + # "optional" dependency + from matplotlib import pyplot as plt + + if 'drawstyle' in kwargs or 'ds' in kwargs: + raise TypeError("cannot set draw style in a spectrum's plot_quick") + + kwargs['drawstyle'] = 'steps-post' + + if len(self.shape) != 1: + nspecdim = len(self.shape) - 1 + indexing_hint = 'spec[' + ', '.join(['0']*nspecdim) + ']' + raise ValueError(f'plot_quick can only be used on 1d spectra. To ' + 'get the first spectrum, try {indexing_hint}') + + if ax is None: + ax = plt.gca() + + # TODO: replace below with self.bin_edges once it is correct + mid_bin_edges = (self.spectral_axis[1:] + self.spectral_axis[:-1])/2 + bin_edges = np.concatenate([(self.spectral_axis[0]*2-mid_bin_edges[0]).ravel(), + mid_bin_edges, + (self.spectral_axis[-1]*2-mid_bin_edges[-1]).ravel()]) + + # for a plot with steps-post, the last horizontal line requires a repeat + # of the last flux value + extended_flux = np.concatenate([self.flux, [self.flux[-1]]]) + + ax.plot(bin_edges, extended_flux, **kwargs) + + if x_name is not None: + sa_unit = self.spectral_axis.unit.to_string(format='latex_inline') + ax.set_xlabel(x_name + f' [{sa_unit}]') + + if y_name is not None: + flux_unit = self.flux.unit.to_string(format='latex_inline') + ax.set_ylabel(y_name + f' [{flux_unit}]') + + return ax From 4f32ac502efee3a718a5f6932d83b43fdb6b90d4 Mon Sep 17 00:00:00 2001 From: Erik Tollerud Date: Fri, 10 Apr 2020 17:00:45 -0400 Subject: [PATCH 2/5] add minimal plot test --- specutils/tests/test_spectrum1d.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/specutils/tests/test_spectrum1d.py b/specutils/tests/test_spectrum1d.py index 07efa7fbb..891465cc0 100644 --- a/specutils/tests/test_spectrum1d.py +++ b/specutils/tests/test_spectrum1d.py @@ -11,6 +11,12 @@ from .conftest import remote_access from ..spectra import Spectrum1D +try: + import matplotlib + HAS_MATPLOTLIB = True +except ImportError: + HAS_MATPLOTLIB = False + def test_empty_spectrum(): spec = Spectrum1D(spectral_axis=[]*u.um, @@ -538,3 +544,10 @@ def test_spectral_axis_direction(): wave = [3, 2, 1] * u.nm spec1d = Spectrum1D(spectral_axis=wave, flux=flux) assert spec1d.spectral_axis_direction == 'decreasing' + + +@pytest.mark.skipif('not HAS_MATPLOTLIB') +def test_plot(): + spec_single_flux = Spectrum1D([1, 2] * u.Jy, [3,4] * u.nm) + ax = spec_single_flux.plot_quick() + assert isinstance(ax, matplotlib.axes.Axes) From 8b250d785ad1baf4a6460db7d897ef488565a862 Mon Sep 17 00:00:00 2001 From: Erik Tollerud Date: Mon, 13 Apr 2020 14:30:27 -0400 Subject: [PATCH 3/5] minor typo-ish fixes --- specutils/spectra/spectrum1d.py | 1 - specutils/tests/test_spectrum1d.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/specutils/spectra/spectrum1d.py b/specutils/spectra/spectrum1d.py index b5ccf714e..2e806ed56 100644 --- a/specutils/spectra/spectrum1d.py +++ b/specutils/spectra/spectrum1d.py @@ -769,7 +769,6 @@ def __repr__(self): return result - def plot_quick(self, ax=None, x_name='spectral axis', y_name='flux', **kwargs): """ diff --git a/specutils/tests/test_spectrum1d.py b/specutils/tests/test_spectrum1d.py index 891465cc0..fe699a167 100644 --- a/specutils/tests/test_spectrum1d.py +++ b/specutils/tests/test_spectrum1d.py @@ -548,6 +548,6 @@ def test_spectral_axis_direction(): @pytest.mark.skipif('not HAS_MATPLOTLIB') def test_plot(): - spec_single_flux = Spectrum1D([1, 2] * u.Jy, [3,4] * u.nm) + spec_single_flux = Spectrum1D([1, 2] * u.Jy, [3, 4] * u.nm) ax = spec_single_flux.plot_quick() assert isinstance(ax, matplotlib.axes.Axes) From e4cfccc4225eb6b8e12e2934602248a08eaffb3c Mon Sep 17 00:00:00 2001 From: Simon Torres Date: Wed, 15 Nov 2023 15:55:51 -0700 Subject: [PATCH 4/5] Renamed plot_quick to plot to override ndcube.plot --- specutils/spectra/spectrum1d.py | 2 +- specutils/tests/test_spectrum1d.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/specutils/spectra/spectrum1d.py b/specutils/spectra/spectrum1d.py index 2e806ed56..0877d2945 100644 --- a/specutils/spectra/spectrum1d.py +++ b/specutils/spectra/spectrum1d.py @@ -769,7 +769,7 @@ def __repr__(self): return result - def plot_quick(self, ax=None, x_name='spectral axis', y_name='flux', + def plot(self, ax=None, x_name='spectral axis', y_name='flux', **kwargs): """ Visualize this spectrum using matplotlib in "histogram style". diff --git a/specutils/tests/test_spectrum1d.py b/specutils/tests/test_spectrum1d.py index fe699a167..b4c0db978 100644 --- a/specutils/tests/test_spectrum1d.py +++ b/specutils/tests/test_spectrum1d.py @@ -549,5 +549,5 @@ def test_spectral_axis_direction(): @pytest.mark.skipif('not HAS_MATPLOTLIB') def test_plot(): spec_single_flux = Spectrum1D([1, 2] * u.Jy, [3, 4] * u.nm) - ax = spec_single_flux.plot_quick() + ax = spec_single_flux.plot() assert isinstance(ax, matplotlib.axes.Axes) From 4ef4dcbce9350ae84b126ad319de3c3af2f83bea Mon Sep 17 00:00:00 2001 From: Erik Tollerud Date: Wed, 15 Nov 2023 15:57:31 -0700 Subject: [PATCH 5/5] add quantity_support call option --- specutils/spectra/spectrum1d.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/specutils/spectra/spectrum1d.py b/specutils/spectra/spectrum1d.py index 0877d2945..a0ccf2c0b 100644 --- a/specutils/spectra/spectrum1d.py +++ b/specutils/spectra/spectrum1d.py @@ -770,7 +770,7 @@ def __repr__(self): return result def plot(self, ax=None, x_name='spectral axis', y_name='flux', - **kwargs): + set_quantity_support=True, **kwargs): """ Visualize this spectrum using matplotlib in "histogram style". @@ -785,6 +785,9 @@ def plot(self, ax=None, x_name='spectral axis', y_name='flux', y_name : str or None The name to use for the y axis (units will be automatically added) or None to not set the y axis label. + set_quantity_support : bool + If True, call `astropy.visualization.quantity_support` to ensure + that the quantities in the plot are properly settable. kwargs are passed into `~matplotlib.axes.Axes.plot`, except for ``drawstyle`` or ``ds``. @@ -798,6 +801,10 @@ def plot(self, ax=None, x_name='spectral axis', y_name='flux', # import is intentionally inside the method to make matplotlib an # "optional" dependency from matplotlib import pyplot as plt + from astropy.visualization import quantity_support + + if set_quantity_support: + quantity_support() if 'drawstyle' in kwargs or 'ds' in kwargs: raise TypeError("cannot set draw style in a spectrum's plot_quick")