Plotting classes and functions

Contents

Plotting classes and functions#

Classes and functions for plotting spectra and SDFITS data

Core Functions and Classes#

Core plotting functions.

dysh.plot.core.check_kwargs(known_kwargs, kwargs)[source]#

Check if kwargs are in known_kwargs

dysh.plot.core.parse_html(s)[source]#

Turn html-styled text from spectral line search to matplotlib mathtext

PlotBase#

Plot a spectrum using matplotlib

class dysh.plot.plotbase.PlotBase[source]#

Bases: object

This class describes describes the common interface to all Plot classes.

Attributes:
axis

The underlying Axes object.

Methods

fmt_scans([abbreviate])

Format the scan numbers.

refresh()

Refresh the plot

savefig(file[, hidebuttons])

Save the plot

has_axes

has_figure

has_selector

show

property axis#

The underlying Axes object.

fmt_scans(abbreviate=True)[source]#

Format the scan numbers.

has_axes()[source]#
has_figure()[source]#
has_selector()[source]#
refresh()[source]#

Refresh the plot

savefig(file, hidebuttons=True, **kwargs)[source]#

Save the plot

Parameters:
filestr

The output file name.

hidebuttonsbool

Hide the buttons in the output.

**kwargsdict or key=value pairs

Other arguments to pass to savefig

show()[source]#

SpecPlot#

Plot a spectrum using matplotlib

class dysh.plot.specplot.MultiSpanSelector(ax, minspan=1, xy0=(0, 0))[source]#

Bases: object

Methods

clear()

Clear all regions.

clear_region([event])

Clear a single region.

clear_regions([event])

Clear all regions and start a new empty region.

disconnect()

Disconnect all event handlers to prevent memory leaks and dangling references.

get_selected_regions([ignore_incomplete])

init_first_span()

Add the first span selector to this object.

set_minspan(minspan)

Set the minimum span in data units for the spans.

set_xy0(xy0)

Set the bottom left coordinates in data units for the spans and their handles.

close

init_span

on_press

on_select

clear()[source]#

Clear all regions.

clear_region(event=None)[source]#

Clear a single region.

clear_regions(event=None)[source]#

Clear all regions and start a new empty region.

close()[source]#
disconnect()[source]#

Disconnect all event handlers to prevent memory leaks and dangling references.

get_selected_regions(ignore_incomplete=True)[source]#
Parameters:
ignore_completebool

If True ignore spans that are smaller than self.minspan.

Returns:
regionslist of tuples

List with edges of the spans as tuples.

init_first_span()[source]#

Add the first span selector to this object.

init_span()[source]#
on_press(event)[source]#
on_select(vmin, vmax)[source]#
set_minspan(minspan: float) None[source]#

Set the minimum span in data units for the spans.

set_xy0(xy0: tuple) None[source]#

Set the bottom left coordinates in data units for the spans and their handles.

class dysh.plot.specplot.SpectrumPlot(spectrum, select=True, **kwargs)[source]#

Bases: PlotBase

The SpectrumPlot class is for simple plotting of a Spectrum using matplotlib functions. Plots attributes are modified using keywords (**kwargs) described below. SpectrumPlot will attempt to make smart default choices for the plot if no additional keywords are given.

Parameters:
spectrumSpectrum

The spectrum to plot

Attributes:
axis

The underlying Axes object.

spectrum

The underlying Spectrum

Methods

annotate_vline(xval[, text, rotation])

Add a single annotated vline to the plot.

clear_overlays([blines, oshows, catalog])

Clear Overlays from the plot.

fmt_scans([abbreviate])

Format the scan numbers.

oshow(spectra[, color, linestyle, label, alpha])

Add spectra to the current plot.

plot([show_header, select, oshow, oshow_kwargs])

Plot the spectrum.

refresh()

Refresh the plot

reset()

Reset the plot keyword arguments to their defaults.

savefig(file[, hidebuttons])

Save the plot

set_xaxis_unit(xunit)

Set the units of the abscissa axis of the plot.

set_yaxis_unit(yunit)

Set the units of the ordinate axis of the plot.

set_ydata(ydata[, keep_unit])

Set the ordinate data values for the plot line.

show_baseline(y, *args, **kwargs)

Show a baseline model with values y.

show_catalog_lines([rotation])

Overlay spectral lines from various catalogs on the plot, with annotations.

update_limits()

Recompute the data limits based on the current artists (relim), and then autoscale the view limits using the data limits (autoscale_view).

clear_lines

default_plot_kwargs

get_selected_regions

has_axes

has_figure

has_selector

show

Other Parameters:
xaxis_unitstr or Unit

The units to use on the x-axis, e.g. “km/s” to plot velocity.

yaxis_unitstr or Unit

The units to use on the y-axis.

xminfloat

Minimum x-axis value, in xaxis_unit.

xmaxfloat

Maximum x-axis value, in yaxis_unit.

yminfloat

Minimum y-axis value, in xaxis_unit.

ymaxfloat

Maximum y-axis value, in yaxis_unit.

xlabelstr

x-axis label.

ylabelstr

y-axis label.

labelstr

Label for legend.

alphafloat

Alpha value for the plot. Between 0 and 1.

gridbool

Show a plot grid or not.

figsizetuple

Figure size (see matplotlib).

linewidthfloat

Line width, default: 2.0.

drawstylestr

Line style, default ‘default’.

colorstr

Line color, c also works.

titlestr

Plot title.

vel_framestr

The velocity frame (see VELDEF FITS Keyword).

doppler_convention: str

The velocity convention (see VELDEF FITS Keyword).

annotate_vline(xval, text='', rotation=0)[source]#

Add a single annotated vline to the plot. Can be cleared with the “catalog” gid.

Parameters:
xvalfloat

X value of the line, in the same units as the plot.

textstr

Associated text for the vline. Defaults to an empty string.

rotationfloat

Rotate the text CCW degrees. Default 0.

clear_lines(gid)[source]#
clear_overlays(blines=True, oshows=True, catalog=True)[source]#

Clear Overlays from the plot.

Parameters:
blinesbool

Remove baseline models overlaid on the plot. Default: True

oshowsbool

Remove other spectra overlaid on the plot. Default: True

catalogbool

Remove catalog spectral lines overlaid on the plot. Default: True

default_plot_kwargs()[source]#
get_selected_regions()[source]#
oshow(spectra, color=None, linestyle=None, label=None, alpha=None)[source]#

Add spectra to the current plot.

Parameters:
spectralist of dysh.spectra.spectrum.Spectrum or dysh.spectra.spectrum.Spectrum

Spectra to add to the plot.

colorlist of valid matplotlib colors or matplotlib color

Colors for the spectra. There must be one element per spectra.

linestylelist of valid matplotlib linestyles or matplotlib linestyle

Linestyles for the spectra. There must be one element per spectra.

labellist of str

Labels for the spectra. There must be one element per spectra.

alphalist of float

Alpha values for the spectra, between 0 and 1. There must be one element per spectra.

plot(show_header=True, select=True, oshow=None, oshow_kwargs=None, **kwargs)[source]#

Plot the spectrum.

Parameters:
show_headerbool

Show informational header.

selectbool

Allow selecting regions via click and drag.

oshowlist or Spectrum

Spectra to overlay in the plot.

oshow_kwargsdict

Dictionary with parameters for SpectrumPlot.oshow. These include color, linestyle, label, and alpha.

Other Parameters:
xaxis_unitstr or Unit

The units to use on the x-axis, e.g. “km/s” to plot velocity.

yaxis_unitstr or Unit

The units to use on the y-axis.

xminfloat

Minimum x-axis value, in xaxis_unit.

xmaxfloat

Maximum x-axis value, in yaxis_unit.

yminfloat

Minimum y-axis value, in xaxis_unit.

ymaxfloat

Maximum y-axis value, in yaxis_unit.

xlabelstr

x-axis label.

ylabelstr

y-axis label.

labelstr

Label for legend.

alphafloat

Alpha value for the plot. Between 0 and 1.

gridbool

Show a plot grid or not.

figsizetuple

Figure size (see matplotlib).

linewidthfloat

Line width, default: 2.0.

drawstylestr

Line style, default ‘default’.

colorstr

Line color, c also works.

titlestr

Plot title.

vel_framestr

The velocity frame (see VELDEF FITS Keyword).

doppler_convention: str

The velocity convention (see VELDEF FITS Keyword).

reset()[source]#

Reset the plot keyword arguments to their defaults.

set_xaxis_unit(xunit: str | Quantity) None[source]#

Set the units of the abscissa axis of the plot.

Parameters:
xunitstr or Quantity

New units for the x-axis. It must be a valid unit for the data.

set_yaxis_unit(yunit: str | Quantity) None[source]#

Set the units of the ordinate axis of the plot.

Parameters:
yunitstr or Quantity

New units for the y-axis. It must be a valid unit for the data.

set_ydata(ydata, keep_unit=False) None[source]#

Set the ordinate data values for the plot line. That is the line that representes the underlying Spectrum.

Parameters:
ydataarray

Ordinate axes values to display. These can be floats or quantities.

keep_unitbool

Only used if ydata is a Quantity. If True, keep the units of ydata. If False, scale to the units of the current plot line.

show_baseline(y, *args, **kwargs) None[source]#

Show a baseline model with values y.

Parameters:
yarray

Ordinate axes values of the baseline model.

argstuple

Arguments passed to plot.

kwargsLine2D properties, optional

Keyword arguments passed to plot.

show_catalog_lines(rotation=0, **kwargs)[source]#

Overlay spectral lines from various catalogs on the plot, with annotations.

Parameters:
rotationfloat, degrees

Rotate the annotation text CCW to aid in readability. Default 0.

**kwargs

All other kwargs get passed to dysh.line.query_lines.

property spectrum#

The underlying Spectrum

update_limits()[source]#

Recompute the data limits based on the current artists (relim), and then autoscale the view limits using the data limits (autoscale_view).

ScanPlot#

Plot a spectrum using matplotlib

class dysh.plot.scanplot.ScanPlot(scanblock_or_scan, **kwargs)[source]#

Bases: PlotBase

The ScanPlot class is for simple plotting of a Scan or ScanBlock using matplotlib functions. Plot attributes are modified using keywords (**kwargs) described below. SpectrumPlot will attempt to make smart default choices for the plot if no additional keywords are given.

Parameters:
scanblock_or_scanScan or ScanBlock

The scan or scanblock to plot.

**kwargsdict

Plot attribute keyword arguments, see below.

Attributes:
axis

The underlying Axes object.

Methods

fmt_scans([abbreviate])

Format the scan numbers.

plot([show_header, spectral_unit])

Plot the scan.

refresh()

Refresh the plot

reset()

Reset the plot keyword arguments to their defaults.

savefig(file[, hidebuttons])

Save the plot

set_clim([vmin, vmax])

Set the vmin and vmax parameters of the image.

set_cmap([cmap])

Set the cmap of the image.

set_interpolation([interpolation])

Set the interpolation of the image.

set_norm([norm])

Set the normalization of the image colormap.

write(filename[, avechan, chan, overwrite])

Write the current spectrogram as a FITS image.

has_axes

has_figure

has_selector

show

Other Parameters:
spectral_unitstr

The units to use on the frequency axis. Can be ‘MHz’ or ‘GHz’.

plot(show_header=True, spectral_unit=None, **kwargs)[source]#

Plot the scan.

Parameters:
spectral_unitUnit

The units to use on the frequency axis. Default: MHz if below 1 GHz, GHz if above. Otherwise, can be any valid frequency unit.

**kwargsvarious

keyword=value arguments drawn from imshow kwargs. Currently implemented kwargs include cmap, interpolation, vmin, vmax, and norm.

reset()[source]#

Reset the plot keyword arguments to their defaults.

set_clim(vmin=None, vmax=None)[source]#

Set the vmin and vmax parameters of the image.

Parameters:
vminfloat

The minimum value of the color scale. Default None; to autoscale.

vmaxfloat

The maximum value of the color scale. Default None; to autoscale.

set_cmap(cmap='inferno')[source]#

Set the cmap of the image.

Parameters:
cmapstr

cmap used for the color scale. Default: “inferno”.

set_interpolation(interpolation='nearest')[source]#

Set the interpolation of the image.

Parameters:
interpolationstr

Interpolation method. Default: “nearest”.

set_norm(norm=None)[source]#

Set the normalization of the image colormap. Can be any value supported by the imshow norm keyword, e.g. ‘linear’, ‘log’ etc or a Normalize object.

Parameters:
normstr | Normalize | None

norm used for the color scale. Default: “None”, for linear scaling.

write(filename, avechan=1, chan=None, overwrite=False)[source]#

Write the current spectrogram as a FITS image. No WCS is maintained yet. The integrations are in the first axis and the channels in the second.

Parameters:
filenamestr

Filename of FITS file to be saved. No default

avechanint

Averaging number of channels. Only supports averaging by a multiple of the number of channels. Default: 1

chan[int,int]

If given, it will select this channel range for output. Default: all channels will be selected

overwriteboolean

Overwrite existing file. Default: False

GUIs#

BaseGUI#

Base class for dysh GUIs

class dysh.plot.basegui.BaseGUI[source]#

Bases: object

Methods

connect_buttons(plotbase)

Connect the GUI button(s) callbacks.

disconnect_buttons()

Disconnect the GUI button(s) callbacks.

is_window_alive()

Is the GUI window alive.

show()

Show the GUI contents.

connect_buttons(plotbase)[source]#

Connect the GUI button(s) callbacks.

Parameters:
plotbasePlotBase

Plotting class that defines the functions to be used in the callbacks.

disconnect_buttons()[source]#

Disconnect the GUI button(s) callbacks.

is_window_alive()[source]#

Is the GUI window alive.

Returns:
True if the GUI window is still alive.
show()[source]#

Show the GUI contents.

ShellGUI#

Graphical User Interface for dysh shell.

class dysh.plot.shellgui.ShellGUI(plotbase)[source]#

Bases: BaseGUI

Tk based GUI. Used for plotting in the shell.

Methods

connect_buttons(plotbase)

Connect the GUI button(s) callbacks.

disconnect_buttons()

Disconnect the GUI button(s) callbacks.

is_window_alive()

Is the GUI window alive.

show()

Show the GUI contents.

connect_buttons(plotbase)[source]#

Connect the GUI button(s) callbacks.

Parameters:
plotbasePlotBase

Plotting class that defines the functions to be used in the callbacks.

disconnect_buttons()[source]#

Disconnect the GUI button(s) callbacks.

is_window_alive()[source]#

Is the GUI window alive.

Returns:
True if the GUI window is still alive.
show()[source]#

Show the GUI contents.

LabGUI#

dysh lab (Jupyter) plotting interface.

class dysh.plot.labgui.LabGUI(plotbase)[source]#

Bases: BaseGUI

NBAgg based GUI. Used for jupyter.

Methods

connect_buttons(plotbase)

Connect the GUI button(s) callbacks.

disconnect_buttons()

Disconnect the GUI button(s) callbacks.

is_window_alive()

Is the GUI window alive.

show()

Show the GUI contents.

connect_buttons(plotbase)[source]#

Connect the GUI button(s) callbacks.

Parameters:
plotbasePlotBase

Plotting class that defines the functions to be used in the callbacks.

disconnect_buttons()[source]#

Disconnect the GUI button(s) callbacks.

is_window_alive()[source]#

Is the GUI window alive.

Returns:
True if the GUI window is still alive.
show()[source]#

Show the GUI contents.

class dysh.plot.labgui.StaticLabGUI(plotbase)[source]#

Bases: BaseGUI

Agg based GUI, i.e., no GUI at all. Used for jupyter rendering in documentation.

Methods

connect_buttons(plotbase)

Connect the GUI button(s) callbacks.

disconnect_buttons()

Disconnect the GUI button(s) callbacks.

is_window_alive()

Is the GUI window alive.

show()

Show the GUI contents.

show()[source]#

Show the GUI contents.

StaticGUI#

dysh headless “GUI”.

class dysh.plot.staticgui.StaticGUI(plotbase)[source]#

Bases: BaseGUI

Agg based GUI, i.e., no GUI at all.

Methods

connect_buttons(plotbase)

Connect the GUI button(s) callbacks.

disconnect_buttons()

Disconnect the GUI button(s) callbacks.

is_window_alive()

Is the GUI window alive.

show()

Show the GUI contents.