From 42b7632b20b9197a17b873a19bf4c095ecdc8e37 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Fri, 12 Apr 2024 12:02:27 +0200 Subject: [PATCH 1/4] add plot recipe for Interpolation object --- examples/interpolation_2d_Riesz_kernel.jl | 5 +---- examples/interpolation_2d_polynomials.jl | 5 +---- src/visualization.jl | 16 ++++++++++++++++ test/test_unit.jl | 1 + 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/examples/interpolation_2d_Riesz_kernel.jl b/examples/interpolation_2d_Riesz_kernel.jl index 928e835e..42bd138a 100644 --- a/examples/interpolation_2d_Riesz_kernel.jl +++ b/examples/interpolation_2d_Riesz_kernel.jl @@ -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)) diff --git a/examples/interpolation_2d_polynomials.jl b/examples/interpolation_2d_polynomials.jl index 13b2fb74..9b20e49f 100644 --- a/examples/interpolation_2d_polynomials.jl +++ b/examples/interpolation_2d_polynomials.jl @@ -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)) diff --git a/src/visualization.jl b/src/visualization.jl index 8d25dfb9..9a9888b7 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -124,6 +124,22 @@ end end end +@recipe function f(itp::Interpolation; x_min = 0.0, x_max = 1.0, N = 50) + 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 = zeros((N, N)) + for i in 1:length(x) + for j in 1:length(y) + z[j, i] = itp([x[i], y[j]]) + end + end + xguide --> "x" + yguide --> "y" + seriestype --> :heatmap # :contourf + x, y, z +end + @recipe function f(nodeset::NodeSet, vals::AbstractVector) if dim(nodeset) == 1 @series begin diff --git a/test/test_unit.jl b/test/test_unit.jl index 7882f278..022c7a6b 100644 --- a/test/test_unit.jl +++ b/test/test_unit.jl @@ -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) From ccdfe84d5c63542a21aea61eca2ea8c8628a0c90 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Fri, 12 Apr 2024 12:08:49 +0200 Subject: [PATCH 2/4] avoid for loop --- src/visualization.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/visualization.jl b/src/visualization.jl index 9a9888b7..519e74b6 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -128,12 +128,7 @@ end 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 = zeros((N, N)) - for i in 1:length(x) - for j in 1:length(y) - z[j, i] = itp([x[i], y[j]]) - end - end + z = reshape(itp.(nodeset), (N, N))' xguide --> "x" yguide --> "y" seriestype --> :heatmap # :contourf From a4067798cf689bbbe3fa513ffc725cb0275004cf Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Fri, 12 Apr 2024 12:09:27 +0200 Subject: [PATCH 3/4] format --- src/visualization.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/visualization.jl b/src/visualization.jl index 519e74b6..4a3e4d5a 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -125,14 +125,14 @@ end end @recipe function f(itp::Interpolation; x_min = 0.0, x_max = 1.0, N = 50) - 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 + 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 end @recipe function f(nodeset::NodeSet, vals::AbstractVector) From 2122914207303452169e8ac09db295ee766c8c94 Mon Sep 17 00:00:00 2001 From: Joshua Lampert Date: Fri, 12 Apr 2024 12:16:13 +0200 Subject: [PATCH 4/4] allow plotting interpolation in 1D --- src/visualization.jl | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/visualization.jl b/src/visualization.jl index 4a3e4d5a..227ff83a 100644 --- a/src/visualization.jl +++ b/src/visualization.jl @@ -125,14 +125,21 @@ end end @recipe function f(itp::Interpolation; x_min = 0.0, x_max = 1.0, N = 50) - 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 + 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)