Question

I am using the plot command in Octave to plot y versus x values. Is there a way such that the graph also shows the (x,y) value of every coordinate plotted on the graph itself?

I've tried using the help command but I couldn't find any such option.

Also, if it isn't possible, is there a way to do this using any other plotting function?

Was it helpful?

Solution

Have you tried displaying a text box at each coordinate?

Assuming x and y are co-ordinates already stored in MATLAB, you could do something like this:

plot(x, y, 'b.');
for i = 1 : numel(x) %// x and y are the same lengths
    text(x(i), y(i), ['(' num2str(x(i)) ',' num2str(y(i)) ')']);
end

The above code will take each point in your graph and place a text box (with no borders) in the format of (x,y) where x and y are the coordinates for all of the points.

Note: You may have to play around with the position of the text boxes, because the above code will place each text box right on top of each pair of co-ordinates. You can play around by adding/subtracting appropriate constants in the first and second parameters of the text function. (i.e. text(x(i) + 1, y(i) - 1, .......); However, if you want something quick and for illustrative purposes, then the above code will be just fine.

OTHER TIPS

Here's a method I've used to do something similar. Generating the strings for the labels is the most awkward part, really (I only had a single number per label, which is a lot simpler)

x = rand(1, 10);
y = rand(1, 10);
scatter(x, y);

% create a text object for each point
t = text(x, y, '');

% generate a cell array of labels - x and y must be row vectors in this case
c = strsplit(sprintf('%.2g,%.2g\n',[x;y]), '\n');
c(end) = [];  % the final \n gives us an extra empty cell, remove it
c = c';  % transpose to match the dimensions of t

% assign each label to each text object
set(t, {'String'}, c);

You may want to play around with various properties like 'HorizontalAlignment', 'VerticalAlignment' and 'Margin' to tweak the label positions to your liking.


After a little more thought, here's an alternative, more robust way to generate a suitable cell array of coordinate labels:

c = num2cell([x(:) y(:)], 2);
c = cellfun(@(x) sprintf('%.2g,%.2g',x), c, 'UniformOutput', false);

There is a very useful package labelpoints in MathWorks File Exchange that does exactly that. It has a lot of convenient features.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top