Skip to content

Commit

Permalink
Add plot recipe for Interpolation object (#35)
Browse files Browse the repository at this point in the history
* add plot recipe for Interpolation object

* avoid for loop

* format

* allow plotting interpolation in 1D
  • Loading branch information
JoshuaLampert authored Apr 12, 2024
1 parent 5fd08e2 commit d90d905
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
5 changes: 1 addition & 4 deletions examples/interpolation_2d_Riesz_kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@ many_nodes = homogeneous_hypercube(N; dim = 2)
p1 = plot(many_nodes, itp)
plot!(p1, many_nodes, f, st = :surface)

x = unique(values_along_dim(many_nodes, 1))
y = unique(values_along_dim(many_nodes, 2))
z = reshape(itp.(many_nodes), (N, N))
p2 = plot(x, y, z, linetype = :contourf)
p2 = plot(itp, st = :contourf)
plot(p1, p2, layout = (2, 1))
5 changes: 1 addition & 4 deletions examples/interpolation_2d_polynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@ many_nodes = homogeneous_hypercube(N; dim = 2)
p1 = plot(many_nodes, itp)
plot!(p1, many_nodes, f, st = :surface)

x = unique(values_along_dim(many_nodes, 1))
y = unique(values_along_dim(many_nodes, 2))
z = reshape(itp.(many_nodes), (N, N))
p2 = plot(x, y, z, linetype = :contourf)
p2 = plot(itp, st = :heatmap)
plot(p1, p2, layout = (2, 1))
18 changes: 18 additions & 0 deletions src/visualization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ end
end
end

@recipe function f(itp::Interpolation; x_min = 0.0, x_max = 1.0, N = 50)
if dim(itp) == 1
x = LinRange(x_min, x_max, N)
x, itp.(x)
elseif dim(itp) == 2
nodeset = homogeneous_hypercube(N, x_min, x_max; dim = 2)
x = unique(values_along_dim(nodeset, 1))
y = unique(values_along_dim(nodeset, 2))
z = reshape(itp.(nodeset), (N, N))'
xguide --> "x"
yguide --> "y"
seriestype --> :heatmap # :contourf
x, y, z
else
@error("Plotting an interpolation is only supported for dimension up to 2, but the interpolation has dimension $(dim(itp))")
end
end

@recipe function f(nodeset::NodeSet, vals::AbstractVector)
if dim(nodeset) == 1
@series begin
Expand Down
1 change: 1 addition & 0 deletions test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ using Plots
itp = interpolate(nodes, ff)
nodes_fine = homogeneous_hypercube(10; dim = dim)
@test_nowarn plot(nodes_fine, itp)
@test_nowarn plot(itp)
if dim == 2
# Test if 2D nodes can be plotted into 3D plot
nodes2d = homogeneous_hypercube(5; dim = 2)
Expand Down

0 comments on commit d90d905

Please sign in to comment.