Plotting helpers
The plotting helpers return Matplotlib figures through PyPlot. Figure widths and heights are generally specified in approximately centimetres, converted to inches internally; font sizes use points. Image-mosaic width means width per tile column, and plt_B0map reserves additional colorbar space.
Use numerical metrics for quantitative comparisons. Plot appearance alone does not establish reconstruction accuracy or correct physical coordinate mapping.
A complete CPU example
This example saves two PNGs in a temporary directory and closes both figures. Set MPLBACKEND=Agg before loading the package when running without a display.
ENV["MPLBACKEND"] = get(ENV, "MPLBACKEND", "Agg")
using HighOrderMRI
import PyPlot
output_dir = mktempdir()
image = reshape(collect(range(0.0, 1.0; length=64)), 8, 8)
image_path = joinpath(output_dir, "image.png")
fig = plt_image(image; title="Numerical example", width=6, vmin=0, vmax=1)
try
fig.savefig(image_path, dpi=120, bbox_inches="tight")
finally
PyPlot.close(fig)
end
# Encoding coefficients are term-first and use cycles; plotting is sample-first
# and its fixed phase labels use radians. Select one readout before this conversion.
phase_cycles = zeros(Float64, 9, 32)
phase_cycles[2, :] .= range(-1.0, 1.0; length=32)
phase_radians = (2pi) .* permutedims(phase_cycles)
phase_path = joinpath(output_dir, "phase.png")
phase_fig = plt_kspha(phase_radians, 1e-5)
try
phase_fig.savefig(phase_path, dpi=120, bbox_inches="tight")
finally
PyPlot.close(phase_fig)
end
@assert isfile(image_path) && isfile(phase_path)
println(output_dir)Shapes, units and existing limitations
plt_kspha/plt_ksphastake(nSample,nTerm)matrices. Their axes assume radians per spatial-order unit and plot the input unchanged. Their time axes start at one dwell, whereas interpolation helpers use a zero time origin.plt_bfield/plt_bfield_complot the first nine columns with fixed field-basis labels and mT/m^p units. They do not convert phase into a magnetic field.- The GIRF forms of
plt_gradaccept Hz/m and convert displayed values to mT/m; the geometry forms document their own coordinate and unit contracts below. - In
plt_image, specifying only one ofvminandvmaxcurrently recomputes both from quantiles. Supply both to retain explicit limits. plt_scattercurrently accepts but does not forwardmarker,marker_sizeormarker_linewidth. Their values do not control the plotted markers.plt_imagesandplt_trajsare declared exports without definitions (PG-11). Use the definedplt_imageoverloads. The loaded, unexportedHighOrderMRI.plt_trajexpects(nPoint,3)columns[kx,ky,kz]and draws only kx/ky; it is not the undefinedplt_trajsexport.
These are documented limits of the current implementation, not fixes introduced by the documentation migration. The unreferenced src/plot/plt_plotscatter.jl file is not loaded and is not presented as an available API.
plt_plot
plt_plot(
ys::AbstractVector{T};
xs = nothing,
labels = nothing,
xlabel = "",
ylabel = "",
width = 15,
height = 8,
axis_equal = false,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2) where T <: AbstractVector{<:Real}Plot a vector of real-valued vectors using Matplotlib and return the figure. xs is a collection of x-coordinate vectors: use one per curve, or a single vector wrapped in a collection to reuse it. Coordinate vectors are sliced to the y length and must be long enough. Without xs, Matplotlib supplies its zero-based index coordinates. Missing/short labels are replaced with blanks.
width and height are approximately centimetres: the implementation divides by 2.53999863 before passing inches to Matplotlib. Font sizes are points. Use PyPlot.close(fig) after saving when creating many figures.
plt_scatter
plt_scatter(
ys::AbstractVector{T};
xs = nothing,
labels = nothing,
xlabel = "",
ylabel = "",
width = 15,
height = 8,
axis_equal = false,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2,
marker = "o",
marker_size = 1.0,
marker_linewidth = 0.5) where T <: AbstractVector{<:Real}Plot a vector of real-valued vectors as scatter series and return the Matplotlib figure. Without xs, coordinates are Julia indices 1:length(y); with xs, provide one coordinate vector per series or a singleton collection reused for all series. Each x/y pair must have matching lengths.
width and height are approximately centimetres; font sizes are points. Currently marker, marker_size, and marker_linewidth are accepted but not passed to scatter. The general linewidth is passed. Do not rely on the three marker-specific keywords changing the figure.
Source: src/plot/plt_scatter.jl
plt_image
Matrix
plt_image(
img::AbstractArray{<:Real, 2};
title = "",
width = 5,
vmaxp = 100,
vminp = 0,
vmax = nothing,
vmin = nothing,
cmap = "gray",
fontsize_title = 10,
color_facecolor = "#ffffff",
color_label = "#000000")Display a real matrix and return the Matplotlib figure. Width is approximately centimetres; height follows the matrix aspect ratio. No magnitude conversion is performed, so pass abs.(image) for a complex MRI image.
If either vmin or vmax is nothing, the implementation recomputes both from vminp/vmaxp quantiles. Supply both explicit limits to retain either one. Font sizes are points; the function does not save or close its figure.
Volume
plt_image(imgs::AbstractArray{<:Real, 3}; dim = 1, nRow = nothing, nCol = nothing, width = 2, kwargs...)Tile a real 3D array using mosaic, then call the matrix plt_image method. dim selects the slice axis. If either grid size is nothing, both nRow and nCol are replaced by factors of the frame count. Width is approximately centimetres per tile column; the final matrix plot receives width*nCol. Remaining keywords are forwarded to the matrix method. Return its figure.
Image collection
plt_image(imgs::Vector{<:AbstractArray}; nRow = nothing, nCol = nothing, width = 2, kwargs...)Tile a nonempty vector of equally sized real images and call matrix plt_image. Elements must be 2D or 3D with a singleton third axis. If either grid size is nothing, both are chosen from the frame count. Width is approximately centimetres per tile column. Remaining keywords reach the matrix method. Return its figure.
plt_B0map
Matrix
plt_B0map(
b0map::AbstractArray{<:Real, 2};
title = "",
width = 2,
vmax = 100,
vmin = -100,
cmap = "jet",
fontsize_label = 9,
fontsize_ticklabel = 8,
pad_labeltick = 2,
linewidth = 0.5,
ticklength = 1.5,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC")Display a real matrix interpreted as a static field map in Hz, with a labelled colorbar, and return the Matplotlib figure. Values are not converted. Width is approximately the image-panel width in centimetres; the figure additionally reserves margins and colorbar space. Color limits are the supplied values and font sizes are points. This helper does not save or close the figure.
Volume
plt_B0map(b0map::AbstractArray{<:Real, 3}; dim = 1, nRow = nothing, nCol = nothing, width = 2, kwargs...)Tile a 3D real field-map array using mosaic, then call the matrix plt_B0map method. dim selects the slice axis. If either grid size is nothing, both are chosen from the frame count. Width is approximately centimetres per tile column; other keywords are forwarded. Values must already be in Hz. Return the figure.
Image collection
plt_B0map(b0map::Vector{<:AbstractArray}; nRow = nothing, nCol = nothing, width = 2, kwargs...)Tile equally sized real field maps from a nonempty vector; elements are 2D or 3D with singleton third axes. If either grid size is nothing, both are chosen from the frame count. Width is approximately centimetres per tile column. Remaining keywords are forwarded to the matrix method. Return its figure.
plt_kspha
plt_kspha(
kspha::AbstractArray{<:Real, 2},
dt::Real;
width = 15,
height = 7,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2)Plot coefficient columns grouped by spherical-harmonic order and return the Matplotlib figure. Input shape is (nSample, 9) or (nSample, 16), unlike the term-first encoding-operator input. Axis labels assume rad, rad/m, rad/m^2 and rad/m^3; the values are plotted unchanged. Convert cycle-based encoding coefficients explicitly when using these labels.
dt is in seconds. Display times are (1:nSample)*dt in milliseconds, so the first point is at one dwell, not zero. Width and height are approximately centimetres; font sizes are points.
plt_ksphas
plt_ksphas(
ksphas::AbstractVector{T},
dt::Real;
labels = nothing,
width = 15,
height = 8,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2) where T <: AbstractArray{<:Real, 2}Compare coefficient matrices, one per acquisition/readout, in a grid of terms. Use sample-first real matrices with 9 or 16 columns and a consistent term count; labels, when supplied, must match the number of matrices. Samples need not have the same count. Return the Matplotlib figure.
Values are unchanged and axes assume rad/m^p, with the zeroth term in rad. Convert cycle-based coefficients yourself. dt is seconds; each series starts at display time dt, and the horizontal axis is milliseconds. Width/height are approximately centimetres.
plt_bfield
plt_bfield(
bfield::AbstractArray{<:Real, 2},
dt::Float64;
width = 15,
height = 7,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2)Plot the first nine columns of a real (nSample, nTerm) field-coefficient matrix, with nTerm >= 9. The fixed legend order is 1, x, y, z, xy, zy, 2z^2-(x^2+y^2), xz, x^2-y^2. Additional columns are ignored.
Values are plotted unchanged; axis labels assume mT, mT/m and mT/m^2, not phase in radians or cycles. No gyromagnetic or phase-to-field conversion is performed. dt must be Float64 seconds. Display times start at dt and are shown in ms. Width/height are approximately centimetres. Return the Matplotlib figure.
Source: src/plot/plt_bfield.jl
plt_bfield_com
plt_bfield_com(
bfield1::AbstractArray{<:Real, 2},
bfield2::AbstractArray{<:Real, 2},
dt::Float64;
width = 15,
height = 8,
fontsize_label = 7,
fontsize_legend = 7,
fontsize_ticklabel = 6,
color_facecolor = "#1F1F1F",
color_label = "#CCCCCC",
linewidth = 0.5,
ticklength = 1.5,
pad_label = 2,
pad_labeltick = 2)Compare the first nine field-coefficient columns in two sample-first real matrices. Both must provide the same sample count and at least nine columns; the implementation derives its time axis from bfield1. The fixed legend and units match plt_bfield: values are unchanged and labels assume mT/m^p.
dt must be Float64 seconds; display times start at one dwell and are shown in ms. Width/height are approximately centimetres. Return the Matplotlib figure.
Source: src/plot/plt_bfield.jl
plt_grad
GIRF matrix
plt_grad(G_DCS_Nominal::AbstractMatrix, model::GIRFModel; rbw::Float64 = 1.0, kwargs...)Predict a nominal device-coordinate gradient with apply_girf, then plot its first-order predicted channels against the input. Accept (3,nPoint) or (nPoint,3); a first dimension of three is selected as the spatial axis. Input gradient values are in Hz/m. rbw is passed to GIRF prediction.
Remaining keywords reach the internal plotting engine: dt::Float64=1e-5 seconds or explicit t controls display time, gamma::Float64=42.575575 MHz/T converts Hz/m to mT/m, and width/height are approximately centimetres. The display dwell is not inferred from the model's frequency labels. Return a Matplotlib figure; it does not provide a numerical validation of the GIRF.
GIRF shots
plt_grad(
G_DCS_Nominal::AbstractArray{T, 3},
model::GIRFModel;
dim_dynamic::Int = 1,
rbw::Float64 = 1.0,
kwargs...) where TPredict shots/readouts independently, then concatenate them for plotting. dim_dynamic selects the shot axis. Supply an unambiguous spatial axis of size three distinct from that axis; the remaining axis holds time samples. A separate prediction is performed before concatenation, not a filter over artificial between-shot joins. This does not remove the GIRF convolution's own boundary assumptions.
Input values are in Hz/m; rbw is passed to apply_girf. Remaining keywords reach the plotting engine, including Float64 dt in seconds (default 1e-5), optional t for the complete concatenated display, and gamma in MHz/T (default 42.575575). Display labels convert values to mT/m. Each shot receives a local-time origin; width/height are approximately centimetres. Return the figure.
Nominal gradient matrix
plt_grad(G_nominal::AbstractMatrix, geo::Geometry; dt::Float64 = 1.0e-5, t::Union{Nothing, AbstractVector} = nothing, boundaries::Vector{Int} = Int[], gamma::Float64 = 42.575575, title = "", width = 24, height = 15, fontsize_title = 13, fontsize_label = 12, color_facecolor = "#FFFFFF", color_label = "#000000")Plot nominal gradient values before polarity mapping, in logical RPS, and after rotation into DCS. This Geometry overload applies coordinate/polarity transforms only; it does not predict a GIRF response or measure actual scanner gradients. The current third panel is labelled "Actual Gradient Waveform (DCS)" even though it contains only transformed nominal values.
Input is a matrix with a three-channel axis; when both axes have length three, axis one is chosen. Values are assumed in Hz/m, divided by gamma*1000 for mT/m display; gamma is in MHz/T. dt and optional t are in seconds, displayed in milliseconds. t overrides dt. Boundaries mark segment endpoints and reset displayed local time; they do not alter data. width and height are in centimetres. Returns a PyPlot figure. Interpretation depends on Geometry's vendor mapping.
Source: MRIGeometry/src/plot/plt_grad.jl
Nominal gradient readouts
plt_grad(G_nominal::AbstractArray{T, 3}, geo::Geometry; dim_dynamic::Int = 1, kwargs...) where TConcatenate a three-dimensional nominal-gradient array into segments for the matrix Geometry plotting method. The actual default dynamic axis is 1. The spatial axis is inferred from a size-three axis outside dim_dynamic; choose an unambiguous layout. Remaining keywords are forwarded and segment boundaries are inserted automatically. No GIRF prediction is performed; the result is a PyPlot figure of coordinate-transformed nominal gradients.
Source: MRIGeometry/src/plot/plt_grad.jl
mosaic
Volume
mosaic(imgs; dim = 1, nRow = 4, nCol = 4)Arrange slices of a three-dimensional array as a row-major 2D tile grid. dim selects the slice axis (1, 2 or 3); use positive integer nRow and nCol. Too few slices are padded with zeros; excess slices are truncated. The output has the input element type and tile height times nRow by tile width times nCol. This function returns an array and does not create a figure.
Image collection
mosaic(imgs::Vector{<:AbstractArray}; nRow = 4, nCol = 4)Tile a nonempty vector of identically sized images. Each element must be a 2D array or a 3D array with a singleton third axis, which is dropped. The images are stacked and passed to the array mosaic method with dim=3. Positive nRow/nCol control padding or truncation; return a 2D array.