Julia plot function array issues
Asked Answered
M

2

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.

Multivalent answered 29/9, 2014 at 10:17 Comment(1)
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.Miserly
S
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.

Satire answered 29/9, 2014 at 11:5 Comment(0)
R
0

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

using plots
plyplot()
Retardment answered 26/1, 2019 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.