arrays - 何时使用转置在 Julia 中绘制轮廓

标签 arrays plot julia interpolation contour

因此,我尝试使用以下代码通过插入 2D 函数来在 Julia 中绘制轮廓:

using Interpolations
using Plots
gr()

xs = 1:0.5:5
ys = 1:0.5:8
# The function to be plotted
f(x, y) = (3x + y ^ 2)
g = Float64[f(x,y) for x in xs, y in ys]

# Interpolate the function
g_int = interpolate(g, BSpline(Quadratic(Line(OnCell()))))

# Scale the interpolated function to the correct grid 
gs_int = scale(g_int, xs, ys)

xc = 1:0.1:5
yc = 1:0.1:5

# Compare the real value and the interpolated value of the function at an arbitrary point
println("gs_int(3.2, 3.2) = ", gs_int(3.2, 3.2))
println("f(3.2, 3.2) = ", f(3.2, 3.2))

# Contour of the interpolated plot
p1 = contour(xs, ys, gs_int(xs, ys), fill=true)
# Real contour of the function
p2 = contour(xc, yc, f, fill=true)

plot(p1, p2)

这显然没有给出正确的轮廓,尽管插值似乎是正确的:

a

通过转置 gs_int(xs, ys) 解决了该问题:

p1 = 轮廓(xs, ys, gs_int(xs, ys)', fill=true)

然后我在 2D 空间中随机生成一些点,并重复相同的过程:

using DelimitedFiles
using Interpolations
using Plots
gr()

data = readdlm("./random_points.txt", Float64)

# Create a dictionary to test different orders of interpolations. 
inter = Dict("constant" => BSpline(Constant()), 
    "linear" => BSpline(Linear()), 
    "quadratic" => BSpline(Quadratic(Line(OnCell()))),
    "cubic" => BSpline(Cubic(Line(OnCell())))
)

x = range(-10, length=64, stop=10)
y = range(-10, length=64, stop=10)

v_unscaled = interpolate(data, inter["cubic"])
v = scale(v_unscaled, x, y)

# The contour of the data points
p0 = contour(x, y, data, fill=true)
display(p0)

# The contour of the interpolated function
p_int = contour(x, y, v(x,y)', fill=true)
display(p_int)

但是,这两个等高线图看起来并不相同。

b

当我删除 v(x,y) 之后的撇号时,这有效:

p_int = 轮廓(x, y, v(x,y), fill=true)

c

现在我不明白了。我什么时候应该应用转置,什么时候不应该这样做?

最佳答案

那是因为在第一个示例中您绘制了一个函数,在第二个示例中您绘制了两个数组。这两个数组不需要转置,因为它们的方向相同。但在第一个示例中,生成数组的方式相对于 Plots 从您传递的二维函数生成数组的方式进行了调换。

当您绘制函数时,Plots 会将结果计算为 g = Float64[f(x,y) for y in ys, x in xs] 而不是相反,就像您所做的那样在你的代码中。有关绘图中转置的详细讨论,请再次参阅 https://github.com/JuliaPlots/Makie.jl/issues/205

关于arrays - 何时使用转置在 Julia 中绘制轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58080730/

相关文章:

javascript - 如何获取数组中出现一定次数的数字

r - 在 R : how to highlight specific areas of interest? 中抛光蜘蛛网图

python - 如何从选择 Bokeh 更改数据源

julia - 复数的通用最大/最小函数

julia - 在 Julia 中,如何正确地对调用者提供的(超)类型的参数进行方法分派(dispatch)?

iphone,如何打印字符数组

javascript - 对包含长度值的数组进行排序

arrays - 初始化参数序列和参数数组之间有区别吗?

r - 如何绘制 3 组比例维恩/欧拉图?

arrays - Julia,使用 findall 重置 3d 数组中的第三行