How can I add labels to my scatter plot?

Hello Matlab experts,
I have problems adding data labels to each data point in my scatter plot. The labels I want to attach are in the first column in my Excelsheet. I would highly appreciate it if someone could help me. I am attaching the code below as well as my data file.
%load the data
t = readtable("dataset.xlsx");
x= t.Exp;
y= t.Sim;
sz=70;
s=scatter(x,y,sz,"filled")
hold on
a = [t.Name]'; b = num2str(a); c = cellstr(b);
dx = 0.1; dy = 0.1;
text(x+dx, y+dy, c, 'Fontsize', 10);

 Accepted Answer

Try something like this —
t = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1263190/dataset.xlsx')
t = 28×3 table
Name Sim Exp ____________ ________ __________ {'Proline' } 0.099683 3.379e-05 {'Arginine'} 0.10872 2.07e-05 {'F108A' } 0.22086 4.6726e-05 {'k0.08' } 0.14222 9.7633e-06 {'F124I' } 0.22086 3.5524e-05 {'L113A' } 0.099683 2.9138e-05 {'WT' } 0.14222 2.6192e-05 {'Aro3+' } 0.063385 1.1637e-05 {'WW' } 0.057063 1.1489e-05 {'Aro2+' } 0.073912 1.5793e-05 {'L123A' } 0.14222 3.7604e-05 {'9acidA' } 0.029263 5.0071e-06 {'k0.19' } 0.14222 2.5374e-05 {'W120A' } 0.22086 5.0562e-05 {'k0.47' } 0.14222 1.4083e-05 {'K+' } 0.025349 5.6813e-05
x= t.Exp;
y= t.Sim;
sz=70;
s=scatter(x,y,sz,"filled");
xlabel('Exp')
ylabel('Sim')
text(x, y, t.Name, 'Vert','bottom', 'Horiz','left', 'FontSize',7)
set(gca, 'XScale','log', 'YScale','log')
I set the axis sclaes to 'log' to increase the separation between the points, and reduced the font size. I thought about rotating the labels, however I doubt that there is any way to avoid some of them overwriting the others.
.

6 Comments

@Star Strider, if we add some space to the x and y values, as OP has done in the code, text doesn't show up on graph.
Do you know why this happens? Or you can only add text exactly where the data points are?
t = readtable("dataset.xlsx");
x= t.Exp;
y= t.Sim;
sz=70;
s=scatter(x,y,sz,"filled");
a = [t.Name]';
text(x+0.5, y+0.5, a, 'Fontsize', 10)
figure
scatter(x,y,sz,"filled")
text(x, y, a, 'Fontsize', 10)
Do you know why this happens?
Yes.
Plotting the points with the (dx,dy) offsets (the red + points here) quickly reveals the problem, that being that the ‘dx’ offset is too large for the axes, and so the labels plot outside the axes. (The x-coordinate values are on the order of , much smaller than the ‘dx’ offset.) They exist, however they simply can’t be seen. (The axes limits don’t expand to accommodate them, as they would for plotted points.)
t = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1263190/dataset.xlsx');
x= t.Exp;
y= t.Sim;
sz=70;
s=scatter(x,y,sz,"filled");
xlabel('Exp')
ylabel('Sim')
dx = 0.1; dy = 0.1;
text(x+dx, y+dy, t.Name, 'Fontsize', 10);
hold on
plot(x+dx, y+dy, 'r+')
hold off
And that’s the reason I used the 'Horiz' and 'Vert' name-value pair arguments instead of the offsets, and the logarithmic axes scaling.
.
Thanks for the explaination (and the info on the name-value pair used)
Now that you pointed it, I feel like an idiot for not noticing it before.
"(The axes limits don’t expand to accommodate them, as they would for plotted points.)"
Any particular reason as to why it doesn't automatically rescale the plot axes to include the text as well? Or is it just done so in the MATLAB language?
My pleasure!
Any particular reason as to why it doesn't automatically rescale the plot axes to include the text as well?
I’m not sure. I suspect that allows the text objects to plot easily outside the axes. This is useful because the axes labels, tick lables, titles, and such are (generally) also text objects. If the axes limits expanded to include them, that would make positioning them much more complicated if it were even possible. (I never gave any thought to that until now.)
.
Thank you so much Star Strider for the help! That did the trick.
I highly appreciate your help. Enjoy the rest of your weekend!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!