How to connect points using plot() in Julia

Viewed 208

I would like to connect points on a cartesian coordinates system, like the image below:

x = 1:10;         
y = rand(10);            
plot(x, y)       

enter image description here

In the image above these points are random and the x axis can only take the values from 1 to 10.
I want to create a similar plot where I can assign the position of specific points ( let us name these points: [x_i, y_i] ) and connect them. Here is an example of some values for [x_i, y_i]


[0,2], [4,-10], [5, 12], [12, 6]

1 Answers

From the docs:

you can plot a line by calling plot on two vectors of numbers.

You can pass as first argument to plot a vector of the x_is, and a vector of the y_is as the second argument. Then plot will draw a line. For example:

plot([0, 4, 5, 12], [2, -10, 12, 6])

If you must take the input as pairs of [x_i, y_i], you can just do a little preprocessing before you plot. E.g.

input = [[0,2], [4,-10], [5,12], [12,6]]

 4-element Vector{Vector{Int64}}:
 [0, 2]
 [4, -10]
 [5, 12]
 [12, 6]

plot([x for (x, y) in input], [y for (x, y) in input])

Both produce the output:

enter image description here

Related