Visualization

FastInterpolations.jl provides built-in Plots.jl recipes for automatic visualization of interpolants. Simply call plot() on any interpolant to generate publication-ready figures.

Quick Start

using FastInterpolations
using Plots

# Create sample data
x = [0.0, 0.7, 1.5, 2.3, 3.0, 4.2, 5.0, 6.0]
y = [0.2, 1.1, 0.6, 1.8, 1.2, 0.4, 1.5, 0.8]

# Create interpolant and plot
itp = cubic_interp(x, y; extrap=:constant)
plot(itp)
Example block output

The recipe automatically generates:

  • Interpolation curve — high-resolution spline visualization
  • Data points — original scatter data (auto-hidden for large datasets)
  • Domain boundaries — dashed vertical lines at x[1] and x[end]
  • Out-of-domain shading — gray regions beyond the data domain (when extrapolation enabled)

Basic Usage

Single-Series Interpolants

plot(
    plot(constant_interp(x, y), title="Constant"),
    plot(linear_interp(x, y), title="Linear"),
    plot(cubic_interp(x, y), title="Cubic"),
    layout=(1, 3), size=(900, 300)
)
Example block output

Multi-Series Interpolants

x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
Y = [sin.(x) cos.(x) sin.(x .+ 1)]  # 3 series

sitp = cubic_interp(x, Y; extrap=:extension)
plot(sitp, title="Multi-Series")
Example block output

Derivative Views

x = range(0, 2π, 15)
itp = cubic_interp(collect(x), sin.(x); extrap=:extension)

plot(
    plot(itp, title="S(x)"),
    plot(deriv1(itp), title="S'(x) [1st-derivative]"),
    plot(deriv2(itp), title="S''(x) [2nd-derivative]"),
    plot(deriv3(itp), title="S'''(x) [3rd-derivative]"),
    layout=(4, 1), size=(900, 700)
)
Example block output

Extrapolation Modes

x = [0.0, 1.0, 2.0, 3.0, 4.0]
y = sin.(x)

plot(
    plot(cubic_interp(x, y; extrap=:none), title="extrap=:none"),
    plot(cubic_interp(x, y; extrap=:constant), title="extrap=:constant"),
    plot(cubic_interp(x, y; extrap=:extension), title="extrap=:extension"),
    plot(cubic_interp(x, y; extrap=:wrap), title="extrap=:wrap"),
    layout=(2, 2), size=(900, 600)
)
Example block output

Advanced Usage

All standard Plots.jl attributes work alongside our custom recipe options.

Plots.jl Attributes

x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
y = [0.0, 0.8, 0.9, 0.4, 0.2, 0.6]
itp = cubic_interp(x, y; extrap=:constant)

plot(itp;
    title="Styled Interpolant",
    xlabel="Time (s)", ylabel="Amplitude",
    ylims=(-1.0, 1.5),
    xlims=(-0.8, 8.5),
    linewidth=3,
    alpha=0.5,
    color=:crimson,
    legend=:topright,
)
Example block output

Recipe Options

In addition to standard Plots.jl attributes, the recipe provides custom options:

OptionTypeDefaultDescription
show_dataBool or nothingnothing (auto)Show data points. Auto-hidden when n ≥ 200
show_boundsBooltrueShow vertical lines at domain boundaries
show_outsideBooltrueShow gray shading for out-of-domain regions
samplesInt or nothingnothing (auto)Curve sample count
domain_marginReal or nothingnothing (auto)Domain extension for visualization
series_idxSymbol, Int, Range, Vector:allWhich series to plot (multi-series only)

Use help_plot() to discover options interactively—it displays options while simultaneously rendering the plot, making it easy to experiment with different settings:

julia> help_plot(itp; samples=500, show_bounds=false)
CubicInterpolant
     show_data::Union{Nothing, Bool} = nothing  # Show original data points (default: auto, hidden when n ≥ 200)
     show_bounds::Bool = false  # Show domain boundary lines
     show_outside::Bool = true  # Show out-of-domain shading
     samples::Union{Nothing, Integer} = 500  # Number of curve samples (default: auto)
     domain_margin::Union{Nothing, Real} = nothing  # Domain extension margin (default: auto)

For detailed usage, see HelpPlots.jl.

Customization Examples

itp = cubic_interp(x, y; extrap=:extension)

plot(
    plot(itp; show_data=false, show_bounds=false, show_outside=false, title="Curve only"),
    plot(itp; samples=1000, title="Dense sampling"),
    plot(itp; domain_margin=3.0, title="Wide margin"),
    layout=(1, 3), size=(900, 300)
)
Example block output

Overlaying Interpolants

p = plot(constant_interp(x, y; extrap=:extension); color=:black, alpha=0.7, lw=2)
plot!(p, linear_interp(x, y; extrap=:extension); show_data=false, show_bounds=false, show_outside=false, color=:blue, alpha=0.7, lw=2)
plot!(p, cubic_interp(x, y; extrap=:extension); show_data=false, show_bounds=false, show_outside=false, color=:red, alpha=0.7, lw=2)
plot!(p; title="Method Comparison", legend=:outertopright, xlims=(-1.5, 6.5), ylims=(-1.0, 2.0), size=(900,500))
Example block output

Series Selection

x = range(0, 2π, 10)
Y = [sin.(x) cos.(x) tan.(x) ./ 5]
sitp = cubic_interp(collect(x), Y; extrap=:extension)

plot(
    plot(sitp; series_idx=:all, title="All series"),
    plot(sitp; series_idx=:first, title="First only"),
    plot(sitp; series_idx=[1, 3], title="Series 1 & 3"),
    layout=(3, 1), size=(900, 900)
)
Example block output

See Also