5

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.

Alexander Ridgers
  • 225
  • 1
  • 3
  • 10
  • This gets at syntax as Toivo's answer indicates. But more generally, the plotting package you use may not have the same syntax you are familiar with. For example, Winston -- which is more MATLAB like -- uses plot(x,y) to plot the points connected by lines, as MATLAB would do and plot(x,y,"o") makes a scatter plot. Whereas Gadfly uses plot(x=x, y=y, Geom.line) to make a line graph and plot(x=x, y=y) to make a scatter plot. Alternatively, PyPlot is more similar to MATLAB than Gadfly. – jverzani Sep 29 '14 at 23:52

2 Answers2

8

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.

Toivo Henningsson
  • 2,689
  • 12
  • 10
0

In case you didn't. Write this before your code:

using plots
plyplot()