Plotting error ("hold all-plots")

plotting

#1

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”)


#2

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.


#3

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…


#4

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


#5

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;


#6

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


#7

Because Julia is column-major.


#8

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


#9

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")


#10

this method also works, thanks!