How can I plot a number over its assigned value?

I read this for plotting letters, how can I plot the number over its assigned value?
x = randi(10, 1, 5); % Create Data y = randi(10, 1, 5); L = strsplit(sprintf('%c\n','A':'E')); % Letter Labels figure(1) plot(x, y, '+r') text(x, y, L(1:length(x)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom') axis([-0.5 10.5 -0.5 10.5 ])

 Accepted Answer

I am not certain what numbers you want to plot. The left subplot plots both (x,y) values of the coordinate, and the right subplot only the y-values. Change the format descriptors to what you want. I used '%.1f' here.
The Code:
figure(1)
subplot(1,2,1)
x = randi(10, 1, 5); % Create Data
y = randi(10, 1, 5);
% L = strsplit(sprintf('%c\n','A':'E')); % Letter Labels
L = strsplit(sprintf('(%.1f, %.1f)\n', [x(:) y(:)]'), '\n');
figure(1)
plot(x, y, '+r')
text(x, y, L(1:length(x)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
axis([-0.5 10.5 -0.5 10.5 ])
subplot(1,2,2)
x = randi(10, 1, 5); % Create Data
y = randi(10, 1, 5);
% L = strsplit(sprintf('%c\n','A':'E')); % Letter Labels
L = strsplit(sprintf('%.1f\n', y(:)), '\n');
figure(1)
plot(x, y, '+r')
text(x, y, L(1:length(x)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
axis([-0.5 10.5 -0.5 10.5 ])
The Plot:

3 Comments

Thank you very much for your quick reply. One more question, what exactly are the 'HorizontalAlignment','center', 'VerticalAlignment','bottom')doing in this case?
I am sorry I wasn't very clear, the question I need answered is how do I plot a list of numbers and label the points not with letters but with sequential numbers on the graph? 1,2,3,4 . . . .
My pleasure.
The 'HorizontalAlignment','center', 'VerticalAlignment','bottom' tell the text function how to position the text object with respect to the x and y coordinates that are the first two arguments to it. In this example, it centeres them above the plotted point.
If you want the points plotted and labeled with numbers in the order they appear in your data, this slight variation in my previous code will work:
x = randi(10, 1, 5); % Create Data
y = randi(10, 1, 5);
L = strsplit(sprintf('%d\n', [1:length(x)]), '\n');
figure(1)
plot(x, y, '+r')
text(x, y, L(1:end-1), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
axis([-0.5 10.5 -0.5 10.5 ])
The only change is in the sprintf arguments:
sprintf('%d\n', [1:length(x)])
Here it creates a vector from 1 to the number of elements in the x vector, and generates a string of numbers that strsplit then separates into a cell array of strings that the text function then plots at the appropriate locations. I used the '%d' format descriptor for them since they are all integers. If the numbers you want to plot as labels are not integers, use some version of the '%f' format descriptor. See the documentation for sprintf for a list and descriptions of the different format descriptors.
Also, I enclosed the vector in square brackets ‘[1:length(x)]’ because that makes the code more readable for me (I know it is a vector just by looking at it). The brackets are not necessary for the code to work correctly. I updated to code so that the text argument for ‘L’ is now L(1:end-1). The last output of the strsplit function is an empty element, and this can cause problems with the x and y and cell array lengths not matching. Eliminating the last empty element prevents that.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!