Source code for marine_qc.plot_qc_outcomes

"""
Plot QC outcomes.

Some plotting routines for QC outcomes
"""

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import figure, lines


[docs] def _get_colours_labels(qc_outcomes: np.ndarray) -> tuple[np.ndarray, list[lines.Line2D]]: """ Get color lebels. Parameters ---------- qc_outcomes : np.ndarray Array containing the QC outcomes, with 0 meaning pass and non-zero entries indicating failure. Returns ------- tuple of (list of str, list of Line2D) Color names and legend elements. """ colour_passed = "#55ff55" colour_failed = "#ff5555" colour_other = "#808080" passed = 0 failed = 0 other = 0 colours_list = [] for outcome in qc_outcomes: if outcome == 0: colours_list.append(colour_passed) passed += 1 elif outcome == 1: colours_list.append(colour_failed) failed += 1 else: colours_list.append(colour_other) other += 1 colours = np.array(colours_list, dtype=str) legend_elements = [ lines.Line2D( [0], [0], marker="o", color="w", label=f"0: {passed}", markerfacecolor=colour_passed, ), lines.Line2D( [0], [0], marker="o", color="w", label=f"1: {failed}", markerfacecolor=colour_failed, ), lines.Line2D( [0], [0], marker="o", color="w", label=f"other: {other}", markerfacecolor=colour_other, ), ] return colours, legend_elements
[docs] def _make_plot( xvalue: np.ndarray, yvalue: np.ndarray, flags: np.ndarray, xlim: list[float] | None, ylim: list[float] | None, xlabel: str, ylabel: str, filename: str | None, ) -> figure.Figure: """ Make plot. Parameters ---------- xvalue : np.ndarray Array of x values. yvalue : np.ndarray Array of y values. flags : np.ndarray Array containing the QC outcomes, with 0 meaning pass and non-zero entries indicating failure. xlim : list of float or None If not None: set xlim for plotting. ylim : list of float or None If not None: set ylim for plotting. xlabel : str Name of the x axis. ylabel : str Name of the y axis. filename : str or None Filename to save the figure to. If None, the figure is saved with a standard name. Returns ------- Figure The main figure obkect created by `plt.subplots()`. """ colours, legend_elements = _get_colours_labels(flags) mask_passed = flags == 0 mask_failed = flags == 1 mask_other = (flags != 0) & (flags != 1) fig, axes = plt.subplots(2, 2, figsize=(16, 9), sharex=True, sharey=True) axes = axes.flatten() titles = ["QC == 0 (Passed)", "QC == 1 (Failed)", "QC == Other", "All Points"] masks = [mask_passed, mask_failed, mask_other, np.ones_like(flags, dtype=bool)] for i in range(4): ax = axes[i] ax.scatter(xvalue[masks[i]], yvalue[masks[i]], c=colours[masks[i]], s=1) ax.set_title(titles[i]) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) if xlim: ax.set_xlim(*xlim) if ylim: ax.set_ylim(*ylim) fig.legend( handles=legend_elements, loc="center", ncol=len(legend_elements), bbox_to_anchor=(0.5, 0.53), ) plt.tight_layout(rect=(0.0, 0.05, 1.0, 1.0)) if filename is None: plt.show(block=False) else: plt.savefig(filename) return fig
[docs] def latitude_variable_plot(lat: np.ndarray, value: np.ndarray, qc_outcomes: np.ndarray, filename: str | None = None) -> figure.Figure: """ Plot a graph of points showing the latitude and value of a set of observations coloured according to the QC oucomes. Parameters ---------- lat : np.ndarray Array of latitude values in degrees. value : np.ndarray Array of observed values for the variable. qc_outcomes : np.ndarray Array containing the QC outcomes, with 0 meaning pass and non-zero entries indicating failure. filename : str or None Filename to save the figure to. If None, the figure is saved with a standard name. Returns ------- Figure The main figure obkect created by `plt.subplots()`. """ return _make_plot( xvalue=value, yvalue=lat, flags=qc_outcomes, xlim=None, ylim=[-90.0, 90.0], xlabel="Variable", ylabel="Latitude", filename=filename, )
[docs] def latitude_longitude_plot(lat: np.ndarray, lon: np.ndarray, qc_outcomes: np.ndarray, filename: str | None = None) -> figure.Figure: """ Plot a graph of points showing the latitude and longitude of a set of observations coloured according to the QC outcomes. Parameters ---------- lat : np.ndarray Array of latitude values in degrees. lon : np.ndarray Array of longitude values in degrees. qc_outcomes : np.ndarray Array containing the QC outcomes, with 0 meaning pass and non-zero entries indicating failure. filename : str or None Filename to save the figure to. If None, the figure is saved with a standard name. Returns ------- Figure The main figure obkect created by `plt.subplots()`. """ return _make_plot( xvalue=lon, yvalue=lat, flags=qc_outcomes, xlim=[-180.0, 180.0], ylim=[-90.0, 90.0], xlabel="Longitude", ylabel="Latitude", filename=filename, )