Plotting 2*N array giving a name to each point

I think this is an extremely simple question, but I just can’t really understand how the plot function reads my inputs.

For instance, I want to plot two points with scatter: (1,2) and (2,3) in a two-dimension vector space.

Each point is an outcome of my function. If I put 0.2 to the function, (1,2) comes out.

For each point, I want to give a label. For instance, for (1,2), the label is “0.2”.

2-element Vector{Any}:
(1, 2)
(2, 3)

Which plotting library are you using? For the most part, points to plot are provided as a vector of x coordinates and a vector of y coordinates. In your case you could do this as:

plot(first.(points), last.(points)

where I’m assuming that points is the 2-element vector of length-2 tuples you posted above.

Annotations vary by plotting package, so again if you want specific advise you’ll need to tell us which package you’re using.

1 Like

Plots.jl is clever enough to recognize a vector of 2-tuples as coordinates. Doing scatter([(1,2),(3,4)]) produces

Thank you very much for the replies.
I’m using Plots.ji.

I’ve tried with the above one, but it returns the following.
This is only half of what I want.

Somehow, the scatter function is perceiving my input only as y1, but what I’m putting in is a 19-element vector of which each row is a two-dimension array. I want to label from y1 to y19, for instance.

One way:

using Plots, Printf
x = LinRange(0,1,19)
v = [(rand(0:9),rand(0:9)) for _ in 1:19]

plot(legend=:outerright)
for (i,t) in pairs(v)
    scatter!(t, label=@sprintf("%.2f",x[i]))
end
Plots.current()
1 Like

I didn’t expect that the problem will be this complicated.
In the end, I could manage to get the following plot. I really appreciate your suggestion.

If I may ask one more thing, I was wondering if there is any way that I can put each label right above each point in the diagram.

Thank you very much again

Yes, see one example here.

1 Like

Thank you very much!

1 Like