trident.spectrum_generator.plot_spectrum

trident.spectrum_generator.plot_spectrum(wavelength, flux, filename='spectrum.png', lambda_limits=None, flux_limits=None, title=None, label=None, figsize=None, step=False, stagger=0.2, features=None, axis_labels=None)[source]

Plot a spectrum or a collection of spectra and save to disk.

This function wraps some Matplotlib plotting functionality for plotting spectra generated with the SpectrumGenerator. In its simplest form, it accepts a wavelength array consisting of wavelength values and a corresponding flux array consisting of relative flux values, and it plots them and saves to disk.

In addition, it can plot several spectra on the same axes simultaneously by passing a list of arrays to the wavelength, flux arguments (and optionally to the label and step keywords).

Returns the Matplotlib Figure object for further processing.

Parameters

Wavelength

array of floats or list of arrays of floats

Wavelength values in angstroms. Either as an array of floats in the case of plotting a single spectrum, or as a list of arrays of floats in the case of plotting several spectra on the same axes.

Flux

array of floats or list of arrays of floats

Relative flux values (from 0 to 1) corresponding to wavelength array. Either as an array of floats in the case of plotting a single spectrum, or as a list of arrays of floats in the case of plotting several spectra on the same axes.

Filename

string, optional

Output filename of the plotted spectrum. Will be a png file. Default: ‘spectrum.png’

Lambda_limits

tuple or list of floats, optional

The minimum and maximum of the wavelength range (x-axis) for the plot in angstroms. If specified as None, will use whole lambda range of spectrum. Example: (1200, 1400) for 1200-1400 Angstroms Default: None

Flux_limits

tuple or list of floats, optional

The minimum and maximum of the flux range (y-axis) for the plot. If specified as None, limits are automatically from [0, 1.1*max(flux)]. Example: (0, 1) for normal flux range before postprocessing. Default: None

Step

boolean or list of booleans, optional

Plot the spectrum as a series of step functions. Appropriate for plotting processed and noisy data. Use a list of booleans when plotting multiple spectra, where each boolean corresponds to the entry in the wavelength and flux lists.

Title

string, optional

Optional title for plot Default: None

Label

string or list of strings, optional

Label for each spectrum to be plotted. Useful if plotting multiple spectra simultaneously. Will automatically trigger a legend to be generated. Default: None

Stagger

float, optional

If plotting multiple spectra on the same axes, do we offset them in the y direction? If set to None, no. If set to a float, stagger them by the flux value specified by this parameter.

Features

dict, optional

Include vertical lines with labels to represent certain spectral features. Each entry in the dictionary consists of a key string to be overplot and the value float as to where in wavelength space it will be plot as a vertical line with the corresponding label.

Example: features={‘Ly a’ : 1216, ‘Ly b’ : 1026}

Default: None

Axis_labels

tuple of strings, optional

Optionally set the axis labels directly. If set to None, defaults to (‘Wavelength [$rmAA$]’, ‘Relative Flux’). Default: None

Returns

Matplotlib Figure object for further processing

Example

Plot a flat spectrum

>>> import numpy as np
>>> import trident
>>> wavelength = np.arange(1200, 1400)
>>> flux = np.ones(len(wavelength))
>>> trident.plot_spectrum(wavelength, flux)

Generate a one-zone ray, create a Lyman alpha spectrum from it, and add gaussian noise to it. Plot both the raw spectrum and the noisy spectrum on top of each other.

>>> import trident
>>> ray = trident.make_onezone_ray(column_densities={'H_p0_number_density':1e21})
>>> sg_final = trident.SpectrumGenerator(lambda_min=1200, lambda_max=1300, dlambda=0.5)
>>> sg_final.make_spectrum(ray, lines=['Ly a'])
>>> sg_final.save_spectrum('spec_raw.h5')
>>> sg_final.add_gaussian_noise(10)
>>> sg_raw = trident.load_spectrum('spec_raw.h5')
>>> trident.plot_spectrum([sg_raw.lambda_field, sg_final.lambda_field],
... [sg_raw.flux_field, sg_final.flux_field], stagger=0, step=[False, True],
... label=['Raw', 'Noisy'], filename='raw_and_noise.png')