Plotting Utilities#

Utility functions for the plotting commands.

lyscripts.plots.floor_at_decimal(value: float, decimal: int) float[source]#

Compute the floor of value for the specified decimal.

Essentially the distance to the right of the decimal point. May be negative.

lyscripts.plots.ceil_at_decimal(value: float, decimal: int) float[source]#

Compute the ceiling of value for the specified decimal.

Analog to floor_at_decimal(), this is the distance to the right of the decimal point. May be negative.

lyscripts.plots.floor_to_step(value: float, step: float) float[source]#

Compute next value on ladder of stepsize step still below value.

lyscripts.plots.ceil_to_step(value: float, step: float) float[source]#

Compute next value on ladder of stepsize step still above value.

lyscripts.plots.clean_and_check(filename: str | Path) Path[source]#

Check if file with filename exists.

If not, raise error, otherwise return cleaned PosixPath.

pydantic model lyscripts.plots.AbstractDistribution[source]#

Abstract class for distributions that should be plotted.

Show JSON schema
{
   "title": "AbstractDistribution",
   "description": "Abstract class for distributions that should be plotted.",
   "type": "object",
   "properties": {
      "scale": {
         "default": 100.0,
         "title": "Scale",
         "type": "number"
      },
      "offset": {
         "default": 0.0,
         "title": "Offset",
         "type": "number"
      },
      "kwargs": {
         "additionalProperties": true,
         "title": "Kwargs",
         "type": "object"
      }
   }
}

field scale: float = 100.0#
field offset: float = 0.0#
field kwargs: dict[str, Any] [Optional]#
abstract draw(axes: MPLAxes) MPLAxes[source]#

Draw the distribution into the provided axes.

abstract left_percentile(percent: float) float[source]#

Compute the point where percent of the values are to the left.

abstract right_percentile(percent: float) float[source]#

Compute the point where percent of the values are to the right.

property label: str#

Return the label of the histogram.

pydantic model lyscripts.plots.Histogram[source]#

Class containing data for plotting a histogram.

Show JSON schema
{
   "title": "Histogram",
   "description": "Class containing data for plotting a histogram.",
   "type": "object",
   "properties": {
      "scale": {
         "default": 100.0,
         "title": "Scale",
         "type": "number"
      },
      "offset": {
         "default": 0.0,
         "title": "Offset",
         "type": "number"
      },
      "kwargs": {
         "additionalProperties": true,
         "title": "Kwargs",
         "type": "object"
      },
      "raw_values": {
         "items": {
            "type": "number"
         },
         "title": "Raw Values",
         "type": "array"
      }
   },
   "required": [
      "raw_values"
   ]
}

field raw_values: NDArray[Shape['*'], float] [Required]#
property values: ndarray#

Return the values of the histogram scaled and offset.

classmethod from_hdf5(filename: str | Path, dataname: str, scale: float = 100.0, offset: float = 0.0, **kwargs) Histogram[source]#

Create a histogram from an HDF5 file.

left_percentile(percent: float) float[source]#

Compute the point where percent of the values are to the left.

right_percentile(percent: float) float[source]#

Compute the point where percent of the values are to the right.

draw(axes: MPLAxes, **defaults) Any[source]#

Draw the histogram into the provided axes.

pydantic model lyscripts.plots.BetaPosterior[source]#

Class for storing plot configs for a Beta posterior.

Show JSON schema
{
   "title": "BetaPosterior",
   "description": "Class for storing plot configs for a Beta posterior.",
   "type": "object",
   "properties": {
      "scale": {
         "default": 100.0,
         "title": "Scale",
         "type": "number"
      },
      "offset": {
         "default": 0.0,
         "title": "Offset",
         "type": "number"
      },
      "kwargs": {
         "additionalProperties": true,
         "title": "Kwargs",
         "type": "object"
      },
      "num_success": {
         "title": "Num Success",
         "type": "integer"
      },
      "num_total": {
         "title": "Num Total",
         "type": "integer"
      }
   },
   "required": [
      "num_success",
      "num_total"
   ]
}

field num_success: int [Required]#
field num_total: int [Required]#
classmethod from_hdf5(filename: str | Path, dataname: str, scale: float = 100.0, offset: float = 0.0, **kwargs) BetaPosterior[source]#

Initialize data container for Beta posteriors from HDF5 file.

property num_fail#

Return the number of failures, i.e. the totals minus the successes.

pdf(x: ndarray) ndarray[source]#

Compute the probability density function.

left_percentile(percent: float) float[source]#

Return the point where the CDF reaches percent.

right_percentile(percent: float) float[source]#

Return the point where 100% minus the CDF equals percent.

draw(axes: MPLAxes, resolution: int = 300, **defaults) Any[source]#

Draw the Beta posterior into the provided axes.

Returns a handle and a label for the legend.

lyscripts.plots.get_size(width='single', unit='cm', ratio='golden')[source]#

Return a tuple of figure sizes in inches.

This is provided as the matplotlib keyword argument figsize expects it. This figure size is computed from a width, in the unit of centimeters by default, and a ratio which is set to the golden ratio by default.

>>> get_size(width="single", ratio="golden")
(3.937007874015748, 2.4332557935820445)
>>> get_size(width="full", ratio=2.)
(6.299212598425196, 3.149606299212598)
>>> get_size(width=10., ratio=1.)
(3.937007874015748, 3.937007874015748)
>>> get_size(width=5, unit="inches", ratio=2./3.)
(5, 7.5)
lyscripts.plots.get_label(attrs: Mapping) str[source]#

Extract label of a histogram from the HDF5 attrs object of the dataset.

lyscripts.plots.get_xlims(contents: AbstractDistributionT, percent_lims: tuple[float] = (10.0, 10.0)) tuple[float][source]#

Get the x-axis limits for a plot containing multiple distribution.

Compute the xlims of a plot containing histograms and probability density functions by considering their smallest and largest percentiles.

lyscripts.plots.draw(axes: MPLAxes, contents: list[AbstractDistribution], percent_lims: tuple[float, float] = (10.0, 10.0), xlims: tuple[float] | None = None, hist_kwargs: dict[str, Any] | None = None, plot_kwargs: dict[str, Any] | None = None) MPLAxes[source]#

Draw histograms and Beta posterior from contents into axes.

The limits of the x-axis is computed to be the smallest and largest left and right percentile of all provided contents respectively via the percent_lims tuple.

The hist_kwargs define general settings that will be applied to all histograms. One additional key 'nbins' may be used to adjust only the numbers, not the spacing of the histogram bins. Similarly, plot_kwargs adjusts the default settings for the Beta posteriors.

Both these keyword arguments can be overwritten by what the individual contents have defined.

lyscripts.plots.split_legends(axes: MPLAxes, titles: list[str], locs: list[tuple[float, float]], **kwargs) None[source]#

Separate labels in axes into separate legends with titles at locs.

lyscripts.plots.use_mpl_stylesheet(file_path: str | Path)[source]#

Load a .mplstyle stylesheet from file_path.

lyscripts.plots.save_figure(output_path: str | Path, figure: Figure, formats: list[str] | None)[source]#

Save a figure to output_path in every one of the provided formats.