Skip to content

Plotting with Makie ​

Install a Makie backend to enable plotting:

julia
using Pkg
Pkg.add(["CairoMakie", "LaTeXStrings"])

Loading the backend activates TransportMapsMakieExt. These examples use CairoMakie.

The shared transportmap_theme combines the ggplot2 styling with LaTeX fonts.

Background, foreground, and data colors ​

The figures on this site automatically follow the documentation's light or dark mode. Light mode uses the original ggplot2 style with gray axis panels and dark text; dark mode uses a transparent background and white text and axes. Theme and export setup is hidden here.

julia
using TransportMaps, CairoMakie, LaTeXStrings, Distributions

backgroundcolor applies to the figure, axes, 3D panels, and legend backgrounds. foregroundcolor controls text, tick marks, axis spines, and reference curves; grid lines use the same color with reduced opacity. SVG and PNG exports preserve transparency.

Set either color to any Makie color, such as :black, :white, or "#202530". Omit both keywords to retain the original ggplot2 colors used in the demo scripts.

Set data colors with each plot's color keyword, independently of the foreground. A tuple such as color=(:steelblue, 0.5) adds opacity. For a custom reference line that should follow the foreground, use lines!(ax, x, y; color=theme(:textcolor)). Themes affect figures created afterwards; recreate existing figures to apply them.

Samples and transformations ​

We set up a simple example using the example from Banana: Map from Density:

sampleplot scatters an N × d sample matrix, with one sample per row. Select two coordinates with dims.

julia
fig = sampleplot(
    X; dims = (1, 2), color = (:steelblue, 1.0),
    axis = (; xlabel = L"z_1", ylabel = L"z_2"),
    figure = (; size = (400, 400))
)
SamplesSamples

transportplot applies a map before plotting the samples:

julia
fig = transportplot(
    M, X; dims = (1, 2), direction = :forward, color = (:purple, 1.0),
    axis = (; xlabel = L"x_1", ylabel = L"x_2"),
    figure = (; size = (400, 400))
)
Samples after the transformSamples after the transform

direction=:forward calls evaluate; :inverse calls inverse. For sample-fitted maps, forward evaluation maps target samples to reference space.

The functions without ! do not create a figure and axis, see sampleplot! and transportplot!

Visualizing the mapping itself ​

mappingplot draws a 1D function curve or a transformed 2D grid. As above, direction selects evaluate or inverse.

One dimension: CDF and inverse CDF ​

See CDF Estimation with a 1D Transport Map for a comparison of logistic-to-uniform and logistic-to-normal maps, their inverses, and their pullback densities. mappingplot! uses direction=:inverse to map the target into reference space. Dashed curves below show the exact maps. Logistic maps with uniform and normal references in both directionsLogistic maps with uniform and normal references in both directions

Two dimensions: transformed grid ​

Pass (xgrid, ygrid) to set the curve resolution and gridlines to set the number of lines per direction. Using normal_map from Banana: Map from Density:

julia
grid = range(-2, 2; length=100)
fig, ax, plt = mappingplot(normal_map, (grid, grid); gridlines=9,
    figure=(; size=(600, 400)),
    axis=(; xlabel=L"x_1", ylabel=L"x_2"))

The full example compares the input grid with its image: A regular reference grid and its curved image under the fitted banana mapA regular reference grid and its curved image under the fitted banana map

For the inverse, use direction=:inverse and a grid in the map's output space. Grid plots support 2D maps, not higher-dimensional slices.

Reference and target comparison ​

reference_target_plot compares reference and target samples for a 2D map. Supply reference samples for density-fitted maps, or target samples for sample-fitted maps. Contours show the fitted density; use density=target for a known density or density=nothing to hide them.

Using the map and samples from Banana: Map from Density:

julia
fig = reference_target_plot(normal_map, normal_ref_samples;
    density=target,
    xgrid=range(-4, 4; length=120),
    ygrid=range(-3, 7; length=120),
    reference_axis=(; limits=((-4, 4), (-4, 4))))
Normal reference samples and transported banana samples over the known target densityNormal reference samples and transported banana samples over the known target density

For the corresponding sample-based construction, see Banana: Map from Samples.

Override the input space with input_space=:reference or :target. Customize the panels through reference_axis and target_axis.

Reference-space diagnostics in higher dimensions ​

referenceplot compares marginals and quantiles with the reference and shows Pearson correlations between coordinates, in any dimension.

Here fitted_map is a sample-fitted four-dimensional map with nonlinear dependencies, trained on 600 synthetic samples. validation_samples contains 1,000 independent target samples. No target density is needed for these plots.

Pass target samples directly: the helper chooses evaluate or inverse and uses the map's configured reference distribution.

julia
marginal_fig = referenceplot(fitted_map, validation_samples; kind = :marginals)
Four mapped validation marginals overlaid with the standard-normal reference PDFFour mapped validation marginals overlaid with the standard-normal reference PDF

In Q–Q plots, agreement follows the dashed diagonal; deviations reveal central or tail differences. Pass already-mapped samples to avoid repeated transformation:

julia
Z = evaluate(fitted_map, validation_samples)
qq_fig = referenceplot(Z; reference = Normal(), kind = :qq)
Reference versus empirical quantiles for four mapped validation coordinatesReference versus empirical quantiles for four mapped validation coordinates

Correlations use a fixed   color scale; gray indicates undefined correlations for constant coordinates.

julia
correlation_fig = referenceplot(Z; reference = Normal(), kind = :correlation)
Pearson correlations between four mapped validation coordinatesPearson correlations between four mapped validation coordinates

For larger maps, select and reorder coordinates and control the panel layout:

julia
referenceplot(fitted_map, validation_samples; kind=:qq, dims=(1, 3, 4), ncols=2)
referenceplot(Z; reference=Normal(), kind=:marginals, dims=(2, 4), bins=30)

Use held-out target samples: round-tripping generated samples only checks inversion. Marginal agreement and zero correlation do not establish independence.

Pairwise scatter-plot matrices ​

plotmatrix combines diagonal histograms with pairwise scatter plots, inspired by MATLAB's plotmatrix.

Reusing the four-dimensional validation samples above:

julia
pairwise_target_fig = plotmatrix(validation_samples; style = :corr)
Four-dimensional target samples with marginal histograms, pairwise scatter plots, and Pearson correlationsFour-dimensional target samples with marginal histograms, pairwise scatter plots, and Pearson correlations

Pass a fitted map to transform samples to reference space and overlay its reference PDF on the diagonal:

julia
pairwise_reference_fig = plotmatrix(fitted_map, validation_samples; style = :corr)
Mapped reference samples with pairwise scatter plots and marginal reference PDF overlaysMapped reference samples with pairwise scatter plots and marginal reference PDF overlays

Choose style=:compact (default) for the lower triangle, :full for all pairs, or :corr for correlations above the diagonal. Reference PDFs use the foreground color; constant-coordinate correlations are labelled undefined.

Select coordinates with dims; dimlabels optionally replaces their labels:

julia
plotmatrix(validation_samples; dims=(1, 3, 4), style=:full)
plotmatrix(Z; reference=Normal(), dims=(2, 4), dimlabels=[L"z_2", L"z_4"])
plotmatrix(fitted_map, validation_samples; space=:target) # no transformation
plotmatrix(fitted_map, Z; input_space=:reference)        # already mapped
plotmatrix(fitted_map, Z; input_space=:reference, space=:target) # generate target samples

Customize with figure=(; size=(600, 600)), axis, bins, markersize, and color.

Term and coefficient contributions ​

termplot shows signed coefficients, labelled by multi-index . Their effect on the map also depends on basis scaling and nonlinear rectification.

Use measure=:removal_rms for the RMS output change when each coefficient is set to zero, without refitting:

Using the fitted map and reference samples from Banana: Map from Density:

julia
fig, ax, plt = termplot(normal_map[2])
fig, ax, plt = termplot(normal_map[2]; measure=:removal_rms,
    samples=normal_ref_samples[1:300, :], figure=(; size=(600, 350)))
Coefficients and removal effects for each term of the second banana-map componentCoefficients and removal effects for each term of the second banana-map component

Removal scores depend on the samples and are not an additive or variance decomposition. The fitted component remains unchanged.

Supply an N × k matrix in forward input coordinates: target for sample-fitted maps, reference for density-fitted maps. For composed maps, apply the linear map first:

julia
Z = evaluate(composed_map.linearmap, target_samples)
termplot(composed_map.polynomialmap[k]; measure=:removal_rms, samples=Z[:, 1:k])

Optimization results ​

convergenceplot plots adaptive objectives by iteration. Using res_best from Banana: Adaptive Transport Map from Samples:

julia
fig, ax, plt = convergenceplot(res_best; show_test=true)
axislegend(ax)
save("convergence.pdf", fig)
Training and test objectives across adaptive iterationsTraining and test objectives across adaptive iterations

The rising test objective in the final iteration indicates overfitting.

For an OptimizationResult, objectiveplot(result) shows objectives per component. Plot separate adaptive component histories with convergenceplot! on separate axes.

Enable show_test=true only when validation data was used; otherwise, stored zeros may be placeholders. Empty or nonfinite test values are omitted.

Update recipes explicitly, e.g. CairoMakie.Makie.update!(plt; dims=(2, 3)). In-place changes to a matrix or map do not notify Makie.

API

See the plotting API reference for all plotting functions and their arguments.


This page was generated using Literate.jl.