xyzpy.plot.plotter_matplotlib#

Functions for plotting datasets nicely.

Module Contents#

Classes#

Functions#

mpl_multi_plot(fn)

Decorate a plotting function to plot a grid of values.

lineplot(ds, x, y[, z, y_err, x_err])

From ds plot lines of y as a function of x, optionally for

auto_lineplot(x, y_z, **lineplot_opts)

Auto version of lineplot() that accepts array arguments

scatter(ds, x, y[, z, y_err, x_err])

From ds plot a scatter of y against x, optionally for

auto_scatter(x, y_z, **scatter_opts)

Auto version of scatter() that accepts array arguments

histogram(ds, x[, z])

Dataset histogram.

auto_histogram(x, **histogram_opts)

Auto version of histogram() that accepts array arguments

heatmap(ds, x, y, z, **kwargs)

From ds plot variable z as a function of x and y using

auto_heatmap(x, **heatmap_opts)

Auto version of heatmap() that accepts array arguments

setup_fig_ax([nrows, ncols, facecolor, rasterize, ...])

show_and_close(fn)

choose_squarest_grid(x)

_compute_hue(z)

to_colors(zs[, magscale, max_mag, alpha_map, alpha_pow])

add_visualize_legend(ax, complexobj, max_mag[, ...])

make_ax_square_after_plotted(ax)

handle_sequence_of_arrays(f)

Simple wrapper to handle sequence of arrays as input to e.g.

visualize_matrix(array[, max_mag, magscale, ...])

Visualize array as a 2D colormapped image.

visualize_tensor(array[, spacing_factor, ...])

Visualize all entries of a tensor, with indices mapped into the plane

Attributes#

class xyzpy.plot.plotter_matplotlib.PlotterMatplotlib(ds, x, y, z=None, y_err=None, x_err=None, **kwargs)[source]#

Bases: xyzpy.plot.core.Plotter

prepare_axes()[source]#
set_axes_labels()[source]#
set_axes_scale()[source]#
set_axes_range()[source]#
set_spans()[source]#
set_gridlines()[source]#
set_tick_marks()[source]#
_cax_rel2abs_rect(rel_rect, cax=None)[source]#

Turn a relative axes specification into a absolute one.

plot_legend(grid=False, labels_handles=None)[source]#

Add a legend

set_mappable()[source]#

Mappale object for colorbars.

plot_colorbar(grid=False)[source]#

Add a colorbar to the data.

set_panel_label()[source]#
show()[source]#
prepare_plot()[source]#

Do all the things that every plot has.

xyzpy.plot.plotter_matplotlib.mpl_multi_plot(fn)[source]#

Decorate a plotting function to plot a grid of values.

class xyzpy.plot.plotter_matplotlib.LinePlot(ds, x, y, z=None, *, y_err=None, x_err=None, **kwargs)[source]#

Bases: PlotterMatplotlib, xyzpy.plot.core.AbstractLinePlot

plot_lines()[source]#
__call__()[source]#
xyzpy.plot.plotter_matplotlib.lineplot(ds, x, y, z=None, y_err=None, x_err=None, **plot_opts)[source]#

From ds plot lines of y as a function of x, optionally for varying z.

Parameters:
  • ds (xarray.Dataset) – Dataset to plot from.

  • x (str) – Dimension to plot along the x-axis.

  • y (str or tuple[str]) – Variable(s) to plot along the y-axis. If tuple, plot each of the variables - instead of z.

  • z (str, optional) – Dimension to plot into the page.

  • y_err (str, optional) – Variable to plot as y-error.

  • x_err (str, optional) – Variable to plot as x-error.

  • row (str, optional) – Dimension to vary over as a function of rows.

  • col (str, optional) – Dimension to vary over as a function of columns.

  • plot_opts – See xyzpy.plot.core.PLOTTER_DEFAULTS.

class xyzpy.plot.plotter_matplotlib.AutoLinePlot(x, y_z, **lineplot_opts)[source]#

Bases: LinePlot

xyzpy.plot.plotter_matplotlib.auto_lineplot(x, y_z, **lineplot_opts)[source]#

Auto version of lineplot() that accepts array arguments by converting them to a Dataset first.

xyzpy.plot.plotter_matplotlib._SCATTER_ALT_DEFAULTS = (('legend_handlelength', 0),)#
class xyzpy.plot.plotter_matplotlib.Scatter(ds, x, y, z=None, **kwargs)[source]#

Bases: PlotterMatplotlib, xyzpy.plot.core.AbstractScatter

plot_scatter()[source]#
__call__()[source]#
xyzpy.plot.plotter_matplotlib.scatter(ds, x, y, z=None, y_err=None, x_err=None, **plot_opts)[source]#

From ds plot a scatter of y against x, optionally for varying z.

Parameters:
  • ds (xarray.Dataset) – Dataset to plot from.

  • x (str) – Quantity to plot along the x-axis.

  • y (str or tuple[str]) – Quantity(s) to plot along the y-axis. If tuple, plot each of the variables - instead of z.

  • z (str, optional) – Dimension to plot into the page.

  • y_err (str, optional) – Variable to plot as y-error.

  • x_err (str, optional) – Variable to plot as x-error.

  • row (str, optional) – Dimension to vary over as a function of rows.

  • col (str, optional) – Dimension to vary over as a function of columns.

  • plot_opts – See xyzpy.plot.core.PLOTTER_DEFAULTS.

class xyzpy.plot.plotter_matplotlib.AutoScatter(x, y_z, **scatter_opts)[source]#

Bases: Scatter

xyzpy.plot.plotter_matplotlib.auto_scatter(x, y_z, **scatter_opts)[source]#

Auto version of scatter() that accepts array arguments by converting them to a Dataset first.

xyzpy.plot.plotter_matplotlib._HISTOGRAM_SPECIFIC_OPTIONS#
xyzpy.plot.plotter_matplotlib._HISTOGRAM_ALT_DEFAULTS#
class xyzpy.plot.plotter_matplotlib.Histogram(ds, x, z=None, **kwargs)[source]#

Bases: PlotterMatplotlib, xyzpy.plot.core.AbstractHistogram

plot_histogram()[source]#
__call__()[source]#
xyzpy.plot.plotter_matplotlib.histogram(ds, x, z=None, **plot_opts)[source]#

Dataset histogram.

Parameters:
  • ds (xarray.Dataset) – The dataset to plot.

  • x (str, sequence of str) – The variable(s) to plot the probability density of. If sequence, plot a histogram of each instead of using a z coordinate.

  • z (str, optional) – If given, range over this coordinate a plot a histogram for each.

  • row (str, optional) – Dimension to vary over as a function of rows.

  • col (str, optional) – Dimension to vary over as a function of columns.

  • plot_opts – See xyzpy.plot.core.PLOTTER_DEFAULTS.

class xyzpy.plot.plotter_matplotlib.AutoHistogram(x, **histogram_opts)[source]#

Bases: Histogram

xyzpy.plot.plotter_matplotlib.auto_histogram(x, **histogram_opts)[source]#

Auto version of histogram() that accepts array arguments by converting them to a Dataset first.

xyzpy.plot.plotter_matplotlib._HEATMAP_ALT_DEFAULTS = (('legend', False), ('colorbar', True), ('colormap', 'inferno'), ('method', 'pcolormesh'),...#
class xyzpy.plot.plotter_matplotlib.HeatMap(ds, x, y, z, **kwargs)[source]#

Bases: PlotterMatplotlib, xyzpy.plot.core.AbstractHeatMap

plot_heatmap()[source]#

Plot the data as a heatmap.

__call__()[source]#
xyzpy.plot.plotter_matplotlib.heatmap(ds, x, y, z, **kwargs)[source]#

From ds plot variable z as a function of x and y using a 2D heatmap.

Parameters:
  • ds (xarray.Dataset) – Dataset to plot from.

  • x (str) – Dimension to plot along the x-axis.

  • y (str) – Dimension to plot along the y-axis.

  • z (str, optional) – Variable to plot as colormap.

  • row (str, optional) – Dimension to vary over as a function of rows.

  • col (str, optional) – Dimension to vary over as a function of columns.

  • plot_opts – See xyzpy.plot.core.PLOTTER_DEFAULTS.

class xyzpy.plot.plotter_matplotlib.AutoHeatMap(x, **heatmap_opts)[source]#

Bases: HeatMap

xyzpy.plot.plotter_matplotlib.auto_heatmap(x, **heatmap_opts)[source]#

Auto version of heatmap() that accepts array arguments by converting them to a Dataset first.

xyzpy.plot.plotter_matplotlib.setup_fig_ax(nrows=1, ncols=1, facecolor=None, rasterize=False, rasterize_dpi=300, figsize=(5, 5), ax=None, **kwargs)[source]#
xyzpy.plot.plotter_matplotlib.show_and_close(fn)[source]#
xyzpy.plot.plotter_matplotlib.choose_squarest_grid(x)[source]#
xyzpy.plot.plotter_matplotlib._compute_hue(z)[source]#
xyzpy.plot.plotter_matplotlib.to_colors(zs, magscale='linear', max_mag=None, alpha_map=True, alpha_pow=1 / 2)[source]#
xyzpy.plot.plotter_matplotlib.add_visualize_legend(ax, complexobj, max_mag, max_projections=2, auto_pad=0.03, legend_loc='auto', legend_size=0.15, legend_bounds=None, legend_resolution=3)[source]#
xyzpy.plot.plotter_matplotlib.make_ax_square_after_plotted(ax)[source]#
xyzpy.plot.plotter_matplotlib.handle_sequence_of_arrays(f)[source]#

Simple wrapper to handle sequence of arrays as input to e.g. visualize_tensor.

xyzpy.plot.plotter_matplotlib.visualize_matrix(array, max_mag=None, magscale='linear', alpha_map=True, alpha_pow=1 / 2, legend=True, legend_loc='auto', legend_size=0.15, legend_bounds=None, legend_resolution=3, facecolor=None, rasterize=4096, rasterize_dpi=300, figsize=(5, 5), ax=None)[source]#

Visualize array as a 2D colormapped image.

Parameters:
  • array (array_like or Sequence[array_like]) – A 2D (or 1D) array or sequence of arrays to visualize.

  • max_mag (float, optional) – The maximum magnitude to use for the color mapping. If not provided, the maximum magnitude in the array will be used.

  • magscale ("linear" or float, optional) – How to scale the magnitude of the array values. If “linear”, then the magnitude is used directly. If a float, then the magnitude is raised to this power before being used, which can help to show variation among small values.

  • alpha_map (bool, optional) – Whether to map the tensor value magnitudes to pixel alpha.

  • alpha_pow (float, optional) – The power to raise the magnitude to when mapping to alpha.

  • legend (bool, optional) – Whether to show a legend (colorbar). If the array has complex dtype then the legend will be a colorwheel.

  • legend_loc (str or tuple[float], optional) – Where to place the legend. If “auto”, then the legend will be placed outside the plot rectangle, otherwise it should be a tuple of (x, y) coordinates in axes space.

  • legend_size (float, optional) – The size of the legend, in relation to the size of the plot axes.

  • legend_bounds (tuple[float], optional) – The bounds of the legend, as (x, y, width, height) in axes space. If not provided, the bounds will be computed from legend_loc and legend_size.

  • legend_resolution (int, optional) – The number of different colors to show in the legend.

  • facecolor (str, optional) – The background color of the plot, by default transparent.

  • rasterize (int or float, optional) – Whether to rasterize the plot. If a number, then rasterize if the number of pixels in the plot is greater than this value.

  • rasterize_dpi (float, optional) – The dpi to use when rasterizing.

  • figsize (tuple[float], optional) – The size of the figure to create, if ax is not provided.

  • ax (matplotlib.Axis, optional) – The axis to draw to. If not provided, a new figure will be created.

  • show_and_close (bool, optional) – If True (the default) then show and close the figure, otherwise return the figure and axis.

Returns:

  • fig (matplotlib.Figure) – The figure containing the plot, or None if ax was provided.

  • ax (matplotlib.Axis) – The axis or axes containing the plot(s).

xyzpy.plot.plotter_matplotlib.visualize_tensor(array, spacing_factor=1.0, max_projections=None, projection_overlap_spacing=1.05, angles=None, scales=None, skew_angle_factor='auto', skew_scale_factor=0.05, max_mag=None, magscale='linear', size_map=True, size_pow=1 / 2, size_scale=1.0, alpha_map=True, alpha_pow=1 / 2, alpha=0.8, marker='o', linewidths=0, show_lattice=True, lattice_opts=None, compass=False, compass_loc='auto', compass_size=0.1, compass_bounds=None, compass_labels=None, compass_opts=None, legend=True, legend_loc='auto', legend_size=0.15, legend_bounds=None, legend_resolution=3, interleave_projections=False, reverse_projections=False, facecolor=None, rasterize=4096, rasterize_dpi=300, figsize=(5, 5), ax=None)[source]#

Visualize all entries of a tensor, with indices mapped into the plane and values mapped into a color wheel.

Parameters:
  • array (numpy.ndarray) – The tensor to visualize.

  • spacing_factor (float, optional) – How to scale the dimensions relative to each other. If 1.0, then each dimension will have the same extent, and smaller dimensions will be sparser. If 0.0, the each dimension will have an extent propoertional to its size, with matching density.

  • max_projections (int, optional) – The maximum number of different projection directions / angles to use. If specified and less than the number of dimensions, then multiple dimensions will share the same angle but with different scales.

  • projection_overlap_spacing (float, optional) – When grouping multiple dimensions to the same angle, how much to increase the spacing at each scale so as to emphasize each.

  • angles (sequence[float], optional) – An explicit list of angles to use for each direction, in radians, with zero pointing straight down. If not provided, then the angles will be calculated automatically.

  • scales (sequence[float], optional) – An explicit list of scales to use for each direction. If not provided, then the scales will be calculated automatically.

  • skew_angle_factor (float, optional) – When there are more than two dimensions, a factor to scale the rotations by to avoid overlapping data points. If 0.0 then the angles will be evenly spaced.

  • skew_scale_factor (float, optional) – When there are more than two dimensions, a factor to scale the scales by to avoid overlapping data points, that shortens non-perpendicular directions.

  • max_mag (float, optional) – The maximum magnitude to use for the color mapping. If not provided, the maximum magnitude in the array will be used.

  • magscale ("linear" or float, optional) – How to scale the magnitude of the array values. If “linear”, then the magnitude is used directly. If a float, then the magnitude is raised to this power before being used, which can help to show variation among small values.

  • size_map (bool, optional) – Whether to map the tensor value magnitudes to marker size.

  • size_scale (float, optional) – An overall factor to scale the marker size by.

  • alpha_map (bool, optional) – Whether to map the tensor value magnitudes to marker alpha.

  • alpha_pow (float, optional) – The power to raise the magnitude to when mapping to alpha.

  • alpha (float, optional) – The overall alpha to use for all markers if not alpha_map.

  • marker (str, optional) – The marker to use for the markers.

  • linewidths (float, optional) – The linewidth to use for the markers.

  • show_lattice (bool, optional) – Show a thin grey line connecting adjacent array coordinate points.

  • lattice_opts (dict, optional) – Options to pass to maplotlib.Axis.scatter for the lattice grid.

  • compass (bool, optional) – Whether to show a compass indicating the orientation of each dimension.

  • compass_loc ((float, float), optional) – Where to place the compass.

  • compass_size (float, optional) – The size of the compass.

  • compass_bounds (tuple[float], optional) – Explicit bounds of the compass, as (x, y, width, height).

  • compass_labels (sequence[str], optional) – Explicit labels for the compass, in order of the dimensions.

  • compass_opts (dict, optional) – Extra options for the compass arrows.

  • legend (bool, optional) – Whether to show a legend (colorbar). If the array has complex dtype then the legend will be a colorwheel.

  • legend_loc (str or tuple[float], optional) – Where to place the legend. If “auto”, then the legend will be placed outside the plot rectangle, otherwise it should be a tuple of (x, y) coordinates in axes space.

  • legend_size (float, optional) – The size of the legend, in relation to the size of the plot axes.

  • legend_bounds (tuple[float], optional) – Explicit bounds of the legend, as (x, y, width, height) in axes space.

  • legend_resolution (int, optional) – The number of different colors to show in the legend.

  • interleave_projections (bool, optional) – If True and grouping dimensions, then they are assigned round robin fashion rather than blocks. False matches the behavior of fusing.

  • reverse_projections (bool, optional) – Whether to reverse the order of the projections.

  • facecolor (str, optional) – The background color of the plot, by default transparent.

  • rasterize (int or float, optional) – Whether to rasterize the plot. If a number, then rasterize if the size of the array is greater than this value.

  • rasterize_dpi (float, optional) – The dpi to use when rasterizing.

  • figsize (tuple, optional) – The size of the figure to create, if ax is not provided.

  • ax (matplotlib.Axis, optional) – The axis to draw to. If not provided, a new figure will be created.

Returns:

  • fig (matplotlib.Figure) – The figure containing the plot, or None if ax was provided.

  • ax (matplotlib.Axis) – The axis containing the plot.