tests.graphics
tests.graphics
Functions
| Name | Description |
|---|---|
| downsample | Downsample a DataArray by striding so that its smallest |
| xrimshow | Plot a 2D DataArray using imshow with consistent absolute margins. |
downsample
tests.graphics.downsample(da, size=500)Downsample a DataArray by striding so that its smallest dimension has between size and 2 x size elements.
Assigns arange coordinates to any dimension that lacks them, so that the strided result preserves meaningful axis values.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| da | xr.DataArray | Input array of any number of dimensions. | required |
| size | int | Target approximate number of elements along the smallest dimension. | 500 |
Returns
| Name | Type | Description |
|---|---|---|
| xr.DataArray | Strided view of the input with reduced resolution. |
xrimshow
tests.graphics.xrimshow(
da,
title=None,
vmin=None,
vmax=None,
cmap=None,
display_size=3.0,
margin_top=0.1,
margin_left=0.6,
margin_bottom=0.5,
margin_right=0.1,
cbar_thickness=0.15,
cbar_length=0.5,
cbar_gap_horizontal=0.5,
cbar_gap_vertical=0.2,
cbar_label_space=0.5,
title_height=0.4,
yincrease=False,
)Plot a 2D DataArray using imshow with consistent absolute margins.
The figure size adapts to the data aspect ratio while maintaining constant image area (width * height = display_size²). A square image is therefore display_size × display_size.
The colorbar is placed below the image for wide data (aspect >= 1) and to the right for tall data (aspect < 1). All margins, gaps and the colorbar thickness are specified in inches and stay constant regardless of data shape, ensuring a visually consistent layout.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| da | xarray.DataArray | 2D DataArray to plot. | required |
| title | str | Title for the plot. | None |
| vmin | float | Colormap range. | None |
| vmax | float | Colormap range. | None |
| cmap | str | Colormap name (e.g., ‘viridis’, ‘plasma’). If None, uses xarray’s default colormap. | None |
| display_size | float | Controls display size. The image area in inches² equals display_size², so a square image would be display_size × display_size. | 3. |
| margin_top | float | Top margin in inches (used when there is no title). | 0.1 |
| margin_left | float | Left margin in inches. | 0.5 |
| margin_bottom | float | Bottom margin in inches. | 0.3 |
| margin_right | float | Right margin in inches. | 0.1 |
| cbar_thickness | float | Colorbar thickness in inches. | 0.15 |
| cbar_length | float | Colorbar length as a fraction of the image extent along the same axis (0 to 1). The colorbar is centered. | 0.5 |
| cbar_gap_horizontal | float | Gap between the image and a horizontal colorbar (below) in inches. | 0.5 |
| cbar_gap_vertical | float | Gap between the image and a vertical colorbar (right) in inches. | 0.2 |
| cbar_label_space | float | Extra space in inches reserved for colorbar tick labels. Added to the right for vertical colorbars, to the bottom for horizontal colorbars. | 0.4 |
| title_height | float | Space reserved for the title in inches (replaces margin_top when title is set). | 0.4 |
| yincrease | bool | If True, the y-axis increases upward. If False, the y-axis increases downward. | False |
Returns
| Name | Type | Description |
|---|---|---|
| fig | matplotlib.figure.Figure | |
| ax | matplotlib.axes.Axes | |
| im | matplotlib.image.AxesImage |