Plotting error ("hold all-plots")

Dear All,

now I am plotting some figures (simple) usig Julia, but It seems not working, i think “hold on” could be the reason??

function Bernstein(n,i,t)

Brnbasis=factorial(BigInt(n))/factorial(BigInt(i))/factorial(BigInt(n-i)) (1/2+t/2)^i(1/2-t/2)^(n-i)
return(Brnbasis)
end

using PyPlot
#using Plots
#pyplot()

n=2 # 2nd/4th order of polynomial
dt=1/50
t=-1:dt:1
y=zeros(size(t))
for i=0:n
for dt=1:size(t,2)
y(dt)=Bernstein(n,i,t(dt))
end
plot(t,y, reuse=false)
end
#xlabel(“X’); ylabel('Y”)

The y(dt) = ... is function declaration syntax in julia, instead use y[dt] = .... I am not familiar with PyPlot but usually if you want hold on MATLAB behaviour , you can use plot!(...) for later plots after first plot in julia plot libraries.

1 Like

Thanks!

I have changed the codes (plot!) as the following:

function Bernstein(n,i,t)

Brnbasis=factorial(BigInt(n))/factorial(BigInt(i))/factorial(BigInt(n-i)) (1/2+t/2)^i (1/2-t/2)^(n-i)
return(Brnbasis)
end

#using PyPlot
using Plots
pyplot()

n=2 # 2nd/4th order of polynomial
dt=1/50
t=-1:dt:1
y=zeros(size(t))
for i=0:n
for dt=1:size(t,1)
y[dt]=Bernstein(n,i,t[dt])
end
plot!(t,y)
end
#xlabel(“X’); ylabel('Y”)

But it shows no result…

A for loop does not return a result.
Add plot!() at the end to see the plot.

1 Like

it shows me one curve in Julia, the correct results are three curves in MATLAB.

In MATLAB is
plot(t,y,‘LineWidth’,2);
hold all;

In addition, why size(t,1) in Julia is equal to :size(t,2) in MATLAB??

Because Julia is column-major.

1 Like

Okay, If I use two times “plot!()”, it shows me correct results, thanks!

1 Like

In Julia, PyPlot will hold on automatically. Just use the correct syntax for indexing and function call.

function Bernstein(n,i,t)
    Brnbasis=factorial(BigInt(n))/factorial(BigInt(i))/factorial(BigInt(n-i)) #(1/2+t/2)^i(1/2-t/2)^(n-i)
    return(Brnbasis)
end

using PyPlot

n  = 2 # 2nd/4th order of polynomial
dt = 1/50
t  = -1:dt:1
y  = zeros(size(t))
for i = 0:n
    for dt = 1:size(t,2)
        y[dt] = Bernstein(n,i,t[dt])
    end
    plot(t,y)
end
xlabel("X"); ylabel("Y")

1 Like

this method also works, thanks!