使用 julia 的 PyPlot 显示图像的地址而不是图像

Using julia's PyPlot shows the address of the image instead of the image

我在Julia中写了一个最小二乘法,想画图的时候提示如下信息(在Vscode中的jupyter中,包含julia扩展。)

PyObject <matplotlib.collections.PathCollection object at 0x00000000017978E0>

我添加了show()命令,但是也不输出图片,提示如下

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

所有代码如下

using PyPlot
function leastsqfit(x::Array,y::Array,n)
    m=length(x)
    d=n+1
    A,b=zeros(d,d),zeros(d,1)
    p=Array{Float64}(undef,2*n+1)
    for k in 1:d
        sum=0
        for i in 1:m
            sum=sum+y[i]*x[i]^(k-1)
        end
        b[k]=sum
    end
    p[1]=m
    for i in 2:2*n+1
        sum =0
        for j in 1:m
            sum=sum+x[j]^(i-1)
        end
        p[i]=sum
    end
    for k in 1:d
        for j in k:d
            A[k,j]=p[k+j-1]
        end
    end
    for i in 2:d
        for j in 1:i-1
            A[i,j]=A[j,i]
        end
    end
    a=A\b
end
function poly(x,A::Array)
    d,sum=length(A),0
    for i in 1:d
        sum=sum+A[i]*x^(i-1)
    end
    return sum
end
function SSE(A::Array,x::Array,y::Array)
    m,sum=length(y),0
    for i in 1:m
        sum=sum+(y[i]-poly(x[i],a))^2
    end
    return sum
end
function Pollt(x::Array,y::Array,n)
    n=length(x)
    a=leastsqfit(xd,yd,1)
    xaxis=x[1]:1/100:x[n]
    yvals=map(x->poly(x,a),xaxis)
    plot(xaxis,yvals)
    scatter(x,y)
end
xd=[1,2,3,4,5,6]
yd=[3,5,9.2,11,14.5,19]
a=leastsqfit(xd,yd,1)
display(a)
Pollt(xd,yd,1)
#show()

我是julia语言的初学者,对以上问题一窍不通,希望大家帮帮我。

抓图可以加

gcf()Pollit 的末尾,这样返回的值将是当前图形,Jupyter 将显示它。

function Pollt(x::Array,y::Array,n)
    n=length(x)
    a=leastsqfit(xd,yd,1)
    xaxis=x[1]:1/100:x[n]
    yvals=map(x->poly(x,a),xaxis)
    plot(xaxis,yvals)
    scatter(x,y)
    gcf()
end

否则在 colling Pollit 之后调用 f = gcf() 将使 f 成为图形,Jupyter 应该在它是单元格的最后一个元素时显示它,或者也调用 display(f) 在单元格中的某个位置。

还有类似的东西

function Pollt(x::Array,y::Array,n)
    n=length(x)
    a=leastsqfit(xd,yd,1)
    xaxis=x[1]:1/100:x[n]
    yvals=map(x->poly(x,a),xaxis)
    plot(xaxis,yvals)
    scatter(x,y)
    f = gcf()
    display(f)
    more_code...
end

应该可以。