How to create a list of (x, y) labels from an n x 2 matrix of x, y data pairs?


I’m using Julia 1.8.5, Plots and the plotly backend. I have an n x 2 matrix of x, y data pairs called “data” which I plot using:

scatter(data[:,1], data[:,2])

I want to annotate each of the plotted data points with its coordinates. Evidently, I can do this as:

annotate!(data[:,1], data[:,2], text.(<array of labels for the points>, :red, :left, 9)

But, I need a way of creating the array of string coordinate pairs; e.g. [“(2, 3)”, “(3.4, 6.1)”, … ]

If you know a better way of accomplishing the main task (annotating the points of a scatter plot with their coordinates), please let me know about it.

Thanks a lot,

For example, string.('(', data[:,1], ", ", data[:,2], ')').

This might be more useful:

string.(Tuple.(eachrow(round.(data, digits=2))))
using Plots; plotly()

data = rand(10,2)
labels = string.(Tuple.(eachrow(round.(data, digits=2))))
Plots.scatter(data[:,1], data[:,2])
Plots.annotate!(data[:,1], data[:,2], labels, :bottom)


That works brilliantly!