plotjuliagadfly

Julia plot function array issues


Having come from Matlab I am struggling to work out why the following does not work:

plot(x=rand(10),y=rand(10))

Produces a graph correctly.

x=rand(10)
y=rand(10)
plot(x,y)

produces error:

ERROR: plot has no method matching plot(::Array(Float64,1),::Array(Float64,1))

I would be very grateful if someone coould explain to me why embeding the code within the plot line produces a result, but defining the variables beforehand results in an error. Logic says they should produce the same result.

I am using Julia v 0.3.1 and have loaded Gadfly as charting tool.


Solution

  • In the first case, you are using keyword argument syntax, not assigning to variables x and y (the meaning of = inside function calls is special). To get the same effect in the second case, you should use

    x=rand(10)
    y=rand(10)
    plot(x=x,y=y)
    

    which passes the value in the variable x in the keyword argument x to plot, and the value in the variable y in the keyword argument y.