zabgrab.services.plotting

What to plot: numeric series grouped by units, split where nothing was fetched.

This module decides the content of a plot; zabgrab.ui.plot draws it with matplotlib. Keeping the two apart means the rules (which items can be plotted, where lines break) are tested without matplotlib.

Functions

histogram_spec(series, bins, ui, *[, title, ...])

Work out a histogram of each numeric series' values.

plot_spec(series, coverage, ui, *[, title, ...])

Work out what to plot.

Classes

Axis(units[, lines, hlines, vlines])

A vertical axis, shared by the series with the same units.

BarAxis(units[, bars, hlines, vlines])

A histogram's subplot, shared by the series with the same units.

Bars(label, counts)

One series' counts in a histogram.

HistogramSpec(bins, axes[, title, log_y])

A histogram to draw: counts of values in bins.

Line(label[, segments])

One series on an axis.

PlotSpec(axes[, title, log_y])

Everything to draw.

RefLine(label, value[, units])

A reference line asked for with --hline or --vline.

Reference(label, at)

A reference line to draw on an axis.

Segment(times, values[, low, high])

A run of points drawn as one connected line.

class zabgrab.services.plotting.Segment(times, values, low=None, high=None)[source]

Bases: object

A run of points drawn as one connected line.

Parameters:
times

POSIX seconds.

Type:

list of float

values

The line’s values: raw values, or the hourly average.

Type:

list of float

low, high

The hourly minimum and maximum, drawn as a band around the line (trends only, when both were fetched).

Type:

list of float or None

class zabgrab.services.plotting.Line(label, segments=<factory>)[source]

Bases: object

One series on an axis.

Parameters:
label

The legend entry, host: name [units].

Type:

str

segments

The connected runs of points; lines break between them.

Type:

list of Segment

class zabgrab.services.plotting.RefLine(label, value, units=None)[source]

Bases: object

A reference line asked for with --hline or --vline.

Parameters:
label

The legend entry: the label given, else the position as given.

Type:

str

value

Where the line goes along its axis: a value (--hline on plots over time, --vline on histograms), a count (--hline on histograms) or POSIX seconds (--vline on plots over time).

Type:

float

units

Draw it only on the axes of items with these units (None for every axis).

Type:

str or None

classmethod parse(text)[source]

Read a line at a number: [LABEL=]VALUE [UNITS].

The label is everything before the last =, so it can contain = itself. Without one, the value (and units) as given label the line.

Parameters:

text (str) – The option’s value, e.g. Max=500, Max=500 #/m3 or 500.

Returns:

RefLine – The line.

Raises:

ValueError – If there’s no value, or it isn’t a finite number.

Return type:

RefLine

Examples

>>> RefLine.parse("Max value=500")
RefLine(label='Max value', value=500.0, units=None)
>>> RefLine.parse("Max temp=26.5 °C")
RefLine(label='Max temp', value=26.5, units='°C')
>>> RefLine.parse("1e3")
RefLine(label='1e3', value=1000.0, units=None)
class zabgrab.services.plotting.Reference(label, at)[source]

Bases: object

A reference line to draw on an axis.

Parameters:
label

The legend entry.

Type:

str

at

Where it goes: a value for lines over time, or a position along the bins for histograms (see zabgrab.services.binning.Bins.position()).

Type:

float

class zabgrab.services.plotting.Axis(units, lines=<factory>, hlines=<factory>, vlines=<factory>)[source]

Bases: object

A vertical axis, shared by the series with the same units.

Parameters:
units

The units of every line on the axis (empty for none).

Type:

str

lines

The series drawn on this axis.

Type:

list of Line

hlines

Horizontal reference lines, at values.

Type:

list of Reference

vlines

Vertical reference lines, at times (POSIX seconds).

Type:

list of Reference

class zabgrab.services.plotting.PlotSpec(axes, title='', log_y=False)[source]

Bases: object

Everything to draw.

Parameters:
axes

One axis per distinct unit, in the order the units first appear.

Type:

list of Axis

title

The figure title (may be empty).

Type:

str

log_y

Whether the vertical axes use a log scale.

Type:

bool

zabgrab.services.plotting.plot_spec(series, coverage, ui, *, title='', log_y=False, hlines=(), vlines=())[source]

Work out what to plot.

Parameters:
  • series (sequence of Series) – The data, e.g. a merged dataset’s series.

  • coverage (sequence of (float, float)) – The time ranges that were fetched, sorted and not overlapping (POSIX seconds). Lines break between ranges, so gaps nobody fetched aren’t drawn as if the data were continuous, while sparse data inside a fetched range is still joined up.

  • ui (UI) – Warns about text items that are left out.

  • title (str, optional) – The figure title.

  • log_y (bool, optional) – Use log scales for the vertical axes. Lines with values of zero or less, which can’t be shown on them, are warned about.

  • hlines (sequence of RefLine, optional) – Horizontal reference lines at values, drawn on every axis or those with their units.

  • vlines (sequence of RefLine, optional) – Vertical reference lines at times (POSIX seconds), drawn on every axis or those with their units.

Returns:

PlotSpec – One axis per unit, each with a line per numeric series.

Raises:

ZabgrabError – If there’s nothing numeric to plot (no series, or only text items).

Return type:

PlotSpec

class zabgrab.services.plotting.Bars(label, counts)[source]

Bases: object

One series’ counts in a histogram.

Parameters:
label

The legend entry, host: name [units].

Type:

str

counts

How many values fall in each bin.

Type:

list of int

class zabgrab.services.plotting.BarAxis(units, bars=<factory>, hlines=<factory>, vlines=<factory>)[source]

Bases: object

A histogram’s subplot, shared by the series with the same units.

Parameters:
units

The units of the values counted (empty for none).

Type:

str

bars

Each series’ counts, drawn side by side in each bin.

Type:

list of Bars

hlines

Horizontal reference lines, at counts.

Type:

list of Reference

vlines

Vertical reference lines, at positions along the bins (see zabgrab.services.binning.Bins.position()).

Type:

list of Reference

class zabgrab.services.plotting.HistogramSpec(bins, axes, title='', log_y=False)[source]

Bases: object

A histogram to draw: counts of values in bins.

Parameters:
bins

The bins’ labels, e.g. < 10.

Type:

list of str

axes

One subplot per distinct unit, in the order the units first appear.

Type:

list of BarAxis

title

The figure title (may be empty).

Type:

str

log_y

Whether the counts use a log scale.

Type:

bool

zabgrab.services.plotting.histogram_spec(series, bins, ui, *, title='', log_y=False, hlines=(), vlines=())[source]

Work out a histogram of each numeric series’ values.

Parameters:
  • series (sequence of Series) – The data.

  • bins (Bins) – The bins (see zabgrab.services.binning.Bins).

  • ui (UI) – Warns about text items that are left out.

  • title (str, optional) – The figure title.

  • log_y (bool, optional) – Use log scales for the counts.

  • hlines (sequence of RefLine, optional) – Horizontal reference lines at counts, e.g. to see which bins exceed a limit, drawn on every axis or those with their units.

  • vlines (sequence of RefLine, optional) – Vertical reference lines at values, placed along the bins (see position()), on every axis or those with their units. Lines inside an open-ended bin are warned about.

Returns:

HistogramSpec – One subplot per unit, each with a set of bars per numeric series.

Raises:

ZabgrabError – If there’s nothing numeric to count (no series, or only text items).

Return type:

HistogramSpec