plot_spectrum#

plot_spectrum(spectrum, ax=None, plot_mode='step', **kwargs)[source]#

Plot all spectra in a Spectrum on a Matplotlib Axes.

Is a thin wrapper of step() or plot(). Plots spectrum.spectral_axis and spectrum.flux. Also sets xpad to 0 by default.

Parameters:
  • spectrum (specutils.Spectrum) – Spectrum to plot. spectrum.flux must be 1D (one line) or 2D with shape (n_spectra, n_spectral) (one line per row). All rows share the same spectral axis.

  • ax (matplotlib.axes.Axes | None, optional, default=None) – Axes to plot on. If None, resolved via get_ax.

  • plot_mode ({'step', 'plot'}, optional, default='step') –

    Plotting backend, case-insensitive.

    • 'step': calls step().

    • 'plot': calls plot().

  • **kwargs – Additional keyword arguments forwarded to the selected backend (visualastro.step or visualastro.plot).

Returns:

Plotted line objects, one per spectrum.

Return type:

list[matplotlib.lines.Line2D]

Raises:
  • TypeError – If spectrum is not a specutils.Spectrum. If ax is not a Matplotlib Axes (raised by get_ax).

  • ValueError – If spectrum.flux has more than 2 dimensions. If plot_mode is not ‘step’ or ‘plot’.

Overview#

  • Plots a specutils.Spectrum (spectral axis vs. flux) on a matplotlib axis.

  • Accepts 1D or 2D flux. For 2D flux, each row is drawn as a separate line.

  • Returns the list of matplotlib.lines.Line2D artists created.

Warning

This requires the optional package specutils. See Installation Guide.

Behavior#

Use plot_mode='step' for a step plot and plot_mode='plot' for a standard line plot. By default, xpad=0 to remove margin padding for the spectral axis (x axis). All remaining **kwargs are forwarded to the underlying plotting function.

Examples#

Step plot of a 1D spectrum:

import matplotlib.pyplot as plt

import visualastro as va
from visualastro.core.io import example_spectrum

SN1987A_N = example_spectrum()

plt.style.use(['full', 'cm'])
va.config.show_type_label = True
va.config.show_unit_label = True

fig, ax = va.subplots(figsize=(13,5))
va.plot_spectrum(SN1987A_N, alpha=0.8, color='darkslateblue', label='NOR')

plt.show()

(Source code, svg)

../../../_images/plot_spectrum.svg