Edit data labels without regenerating plot
Show older comments
I have a plot with labeled data points, but I don't like the way the labels are positioned. Is there a simple and systematic way to move the labels without regenerating the plot, something like 'set', but for the data labels? I have not stored the data or the text handles, and the plot takes a long time to generate, so it is not possible to use the 'text' command again. Furthermore, I do not know how the plot will look until after it has been generated, so it is difficult to specify positions a priori.
For reference, the attached MWE is generated by:
x = 0:pi/36:2*pi; y = sin(x); plot(x,y)
for i = 0:8
textstr = sprintf('P%g',i);
text(i/4*pi,sin(i/4*pi),textstr,'Rotation',45)
end
What I would like is a way to "grab" all the data labels at once in a handle 'T' and 'set(T,'Rotation',0)'. But I cannot generate the plot or the data labels again-- I assume that all I have is the figure. I do not want to have to edit each data label individually (there could be hundreds of them).
(Obviously, the aforementioned issues don't apply to such a small example, but please pretend.)
Answers (2)
KSSV
on 15 Jan 2019
x = 0:pi/36:2*pi; y = sin(x); plot(x,y)
T = gobjects(8,1) ;
for i = 1:8
textstr = sprintf('P%g',i);
T(i) = text(i/4*pi,sin(i/4*pi),textstr,'Rotation',45) ;
end
set(T,'Rotation',0) ;
1 Comment
Kathryn Lund
on 15 Jan 2019
Star Strider
on 15 Jan 2019
See if setting the 'VerticalAlignment' and 'HorizontalAlignment' paramters helps.
Example —
x = 0:pi/36:2*pi;
y = sin(x);
plot(x,y)
XL = xlim;
YL = ylim;
for i = 0:8
textstr = sprintf('P%g',i);
xt = i/4*pi;
yt = sin(i/4*pi);
HA = 'right';
VA = 'middle';
if yt > 0.9*YL(2)
VA = 'top';
HL = 'left';
elseif yt < 0.9*YL(1)
VA = 'bottom';
HA = 'center';
end
text(xt, yt, textstr,'Rotation',45, 'VerticalAlignment',VA, 'HorizontalAlignment',HA)
end
You could use the same idea to ‘tweak’ the ‘xt’ and ‘yt’ values if you want.
8 Comments
Kathryn Lund
on 15 Jan 2019
Star Strider
on 15 Jan 2019
My code doesn’t regenerate the plot. It uses whatever existing data you have in order to position the text objects with respect to the points.
‘Again, this is not what I'm looking for.’
We have no idea what you are looking for.
If you have a specific problem with the text label positions, describe the problem in some detail and upload the plot image so we can see it.
Please understand that ambiguous Questions will produce ambiguous Answers. We don’t know what problems you’re having if you don’t tell us.
Kathryn Lund
on 15 Jan 2019
Star Strider
on 15 Jan 2019
Yes. That’s a frequent problem here on Answers.
‘Your solution requires me to call 'text' again, so in my view, it requires me to regenerate the plot.’
I disagree. The plot is created once, then the text objects are added in the loop. It positions the text objects according to the ‘xt’ and ‘yt’ values you set (and you can ‘tweak’ them to be slightly different from the values on the curve), as well as using the ‘alignment’ options. So the curve is created once, and the text objects are created once, in the loop. This is similar to the original code yopu posted, simply adding logic to adjust the locations and alignments of the text objects.
‘Does that make sense?’
Unfortunately, no. Did you try my code (or some version of it that places the text objects near to where you want them)?
Kathryn Lund
on 15 Jan 2019
Star Strider
on 15 Jan 2019
The ‘MWE.fig’ file is the same as the plot produced in the code you posted.
‘My issue is not how to use text options; it is that I cannot call text again.’
You should be able to call the text function whenever you need to. You may need to use the axes handle of the axes you want to add the text object to, if you later want to change something or have plotted on another axes object in the interim. If something is prohibiting you from doing that, check to see that you don’t have your own function, script, or a variable named ‘text’ on your MATLAB search path or in your workspace. To check that, run this line from your Command Window or a script:
which text -all
or:
which('text','-all')
You should only get this result:
built-in (C:\Program Files\MATLAB\R2018b\toolbox\matlab\graph2d\text)
If neither of these or my other suggestions work, you may need to use the Contact Us link in the upper right corner of this page to request MathWorks Technical Support.
‘One should assume that the data labels are given a priori.’
I don’t. I create them when I need to.
Kathryn Lund
on 17 Jan 2019
Star Strider
on 17 Jan 2019
I’ve been guessing all along as to the problem you want to solve. Without knowing exactly what the problem is, I can’t suggest an applicable soluition.
It seems to me that the code you’re working with is likely not optimally written. Efficient code should allow you to alter the text labels.
The only solution I can guess at is to save your plotted figures as .fig files (so you don’t have to re-create them each time). Then add the text labels, and save those to .fig files with slightly different names from the original .fig files.
That’s how I would do it.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!