spectral_vlines#
- spectral_vlines(x, labels=None, *, y=None, ax=None, color=_Unset.UNSET, unlabeled_color=_Unset.UNSET, linewidth=_Unset.UNSET, linestyle=_Unset.UNSET, alpha=_Unset.UNSET, unit=None, equivalencies=None, fontsize=_Unset.UNSET, rotation=_Unset.UNSET, rotation_mode=_Unset.UNSET, ha=_Unset.UNSET, va=_Unset.UNSET, label_offset_points=_Unset.UNSET, label_kw=None, **kwargs)[source]#
Plot full-height vertical lines at spectral line positions with optional labels.
- Parameters:
x (float | astropy.units.Quantity | Sequence[float | astropy.units.Quantity]) – Line positions. A scalar is treated as a single line.
labels (str | Sequence[str | None] | None, optional, default=None) –
str: broadcast to every line.sequence: one entry per line (length 1 or
len(x)).None: no labels.
None,'', whitespace-only strings and'none'(case-insensitive) mark a line as unlabeled. Unlabeled lines get no annotation.y (float | Quantity | Sequence[float | Quantity | None] | None, optional, default=None) – Data y-coordinate of the label anchor.
Quantityvalues are used as their bare values, without conversion. Scalar is broadcast; a sequence has length 1 orlen(x).None(or aNoneentry) anchors the label at the top of the axes (axes fraction 1.0).ax (matplotlib.axes.Axes | None, optional, default=None) – Axes on which to draw. If
None, usesplt.gca().color (ColorType | str | list[ColorType] | _Unset, optional, default=_UNSET) –
Color of lines and label text. Lines with identical labels share a color.
single color (
str| RGB(A)tuple): all lines.colorset alias (
str): palette assigned to unique labels in order of first appearance, cycled or cropped.list|numpy.ndarray:len(color) == len(x): one color per line, used verbatim. Identical labels are not grouped.otherwise: palette assigned to unique labels in order of first appearance, cycled if shorter than the number of unique labels and cropped if longer. Identical labels share a color.
If
_UNSET, usesconfig.axline.color.unlabeled_color (ColorType | _Unset, optional, default=_UNSET) – Color of unlabeled lines when
coloris a colorset or a palettelist. Ignored ifcoloris a single color or a per-linelist. If_UNSET, usesconfig.spectral_line_marker.unlabeled_color.linewidth (float | list[float] | _Unset, optional, default=_UNSET) – Scalar or per-line (
list) width. If_UNSET, usesconfig.spectral_line_marker.linewidth.linestyle (LineStyleType | list[LineStyleType] | _Unset, optional, default=_UNSET) – Scalar or per-line (
list) style. If_UNSET, usesconfig.spectral_line_marker.vline_linestyle.alpha (float | list[float] | _Unset, optional, default=_UNSET) – Scalar or per-line (
list) alpha. If_UNSET, usesconfig.axline.alpha.unit (astropy.units.UnitBase | None, optional, default=None) – Unit to convert
xto. IfNone,Quantityvalues are used as their bare values, without conversion.equivalencies (astropy.units.Equivalency | None, optional, default=None) – Equivalencies used in the unit conversion of
x.fontsize (float | _Unset, optional, default=_UNSET) – Label font size. If
_UNSET, usesconfig.fontsizes.resolve('text').rotation (float | _Unset, optional, default=_UNSET) – Label rotation in degrees (positive turns CCW). If
_UNSET, usesconfig.spectral_line_marker.label_rotation.rotation_mode ({'default', 'anchor'} | _Unset, optional, default=_UNSET) –
Rotation order for the label (matplotlib
rotation_mode).'default': rotate the text, then align its bbox withhaandva.'anchor': align withhaandva, then rotate about the pinned point.
If
_UNSET, usesconfig.spectral_line_marker.label_rotation_mode.ha ({'left', 'center', 'right'} | _Unset, optional, default=_UNSET) – Horizontal alignment of the label. Selects which point of the text bbox is pinned to the anchor plus offset. If
_UNSET, usesconfig.spectral_line_marker.label_ha.va ({'top', 'bottom', 'center', 'baseline'} | _Unset, optional, default=_UNSET) – Vertical alignment of the label. Selects which point of the text bbox is pinned to the anchor plus offset (
'top': text hangs below the anchor). If_UNSET, usesconfig.spectral_line_marker.label_va.label_offset_points (tuple[float, float] | _Unset, optional, default=_UNSET) – Signed
(dx, dy)shift of the label in points, applied after anchoring. Positivedxmoves right, positivedymoves up, independent ofha,vaandrotation. If_UNSET,dxisconfig.spectral_line_marker.vline_label_offset_points[0]anddyis chosen fromva('top': -4,'bottom'and'baseline': 4,'center': 0) so the label clears the anchor.label_kw (Mapping[str, Any] | None, optional, default=None) – Extra keyword arguments for
ax.annotate. Override label defaults.**kwargs – Forwarded to
_axlines()(and from there toax.axvline).
- Returns:
lines (list[matplotlib.lines.Line2D]) – One per
xvalue, in input order.annotations (list[matplotlib.text.Annotation]) – One per non-
Nonelabel, in input order.
Overview#
Plot a spectrum and mark known emission lines with labeled vertical lines.
Pass the line positions and labels as parallel sequences, and use y to anchor the
labels at a data coordinate just above the spectrum.
Label Placement#
Pass the line positions and labels as parallel sequences. By default (y=None) the
labels are anchored at the top of the axes, independent of the data limits.
import matplotlib.pyplot as plt
import visualastro as va
from visualastro.core.io import example_spectrum
SN1987A_N = example_spectrum()
emission_labels = [
'[Ar II]', '[Ne VI]', '[Ar III]', '[S IV]',
'[Ne II]', '[Ne V]', '[Ne III]', '[S III]'
]
line_locs = [
6.98524305, 7.65248305, 8.99228373, 10.51038726,
12.81370507, 14.32125558, 15.55381827, 18.71226999
]
plt.style.use(['full', 'cm'])
va.config.show_type_label = True
va.config.show_unit_label = True
va.set_plot_cycle(colors=['darkslateblue', 'mediumvioletred'])
fig, ax = va.subplots(figsize=(13,5))
va.plot_spectrum(SN1987A_N, alpha=0.8, label='NOR')
va.spectral_vlines(line_locs, emission_labels, alpha=0.5)
va.set_limits(ylim=(-0.0015, 0.012))
plt.show()
(Source code, svg)
Pass y to anchor the labels at data coordinates instead. A scalar is applied to every line,
and a sequence gives one anchor per line, e.g. fitted peak amplitudes. A None entry in the
sequence falls back to the top of the axes for that line.
Label text placement is further controlled by:
haandvaselect which point of the text bounding box is pinned to the anchor. For example,va='top'makes the text hang below the anchor.rotationsets the label angle in degrees (counter-clockwise).rotation_modesets the order of rotation and alignment. With'default', the text is rotated first and the rotated bounding box is then aligned viahaandva. With'anchor', the text is aligned first and then rotated about the pinned point, so the pin stays fixed at any angle.label_offset_pointsis a signed(dx, dy)shift in points, applied after anchoring. Positivedxmoves the label right and positivedymoves it up, regardless ofha,vaorrotation.
import matplotlib.pyplot as plt
import visualastro as va
from visualastro.core.io import example_spectrum
SN1987A_N = example_spectrum()
emission_labels = ['[Ne II]', '[Ne V]', '[Ne III]', '[S III]']
line_locs = [12.81370507, 14.32125558, 15.55381827, 18.71226999]
amplitudes = [0.005332791, 0.00200568, None, None]
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', xlim=(12,19), ylim=(-0.001, 0.012)
)
va.spectral_vlines(
line_locs, emission_labels, y=amplitudes,
alpha=0.5,
ls=':',
va='top',
ha='right',
label_offset_points=(-5,-10),
rotation=0
)
plt.show()
(Source code, svg)
Changing the Color and Style#
color accepts a single color, a VisualAstro colorset, or a list of colors.
A colorset, or a list whose length differs from the number of lines, is a palette
assigned to the unique labels in order of first appearance. It is cycled or cropped as
needed, so lines with identical labels always share a color. A list with exactly one
color per line is used as given, without grouping. The label text inherits the color of
its line.
Unlabeled lines (None, '' or 'none') are drawn in unlabeled_color when a
colorset or palette is used.
In this example we plot 8 lines with 4 unique labels and one blank label.
import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
from astropy.modeling import models
from specutils import Spectrum
import visualastro as va
np.random.seed(13)
amplitudes = [2.0, 0.6, 3.0, 1.0, 4.0, 0.5, 2.0, 2.5]
line_wave = [4102.89, 4150, 4341.68, 4364.436, 4862.68, 4932.603, 4960.295, 5008.240]
labels = ['Hδ', None, 'Hγ', 'O III', 'Hβ', 'O III', 'O III', 'O III']
gaussians = [models.Gaussian1D(A, mu, 3.0) for (A, mu) in zip(amplitudes, line_wave)]
x = np.linspace(4050, 5050, 500)
y = (
1.0 + 2e-4 * (x - 4050)
+ sum(g(x) for g in gaussians)
+ np.random.normal(0.0, 0.15, x.shape)
)
spectrum = Spectrum(flux=y * u.Jy, spectral_axis=x * u.AA)
plt.style.use(['full', 'cm'])
fig, ax = va.subplots(figsize=(12,6))
va.plot_spectrum(spectrum, color='cobalt')
va.spectral_vlines(
line_wave, labels,
color=['4 train', '7 train', '3 train', 'C train'],
unlabeled_color='L train',
lw=1,
rotation=90,
label_offset_points=(8,-4)
)
va.set_limits(ylim=(0, 6))
plt.show()
(Source code, svg)
Other line properties such as linewidth, linestyle and alpha accept a single
value or a list with one value per line.