Edit data labels without regenerating plot

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.)

回答(2 个)

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 个评论

So, the problem with this solution is that I would have had to store T before generating my plots. I want to access the text objects after the fact, from the figure itself, which I have saved.

请先登录,再进行评论。

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 个评论

Again, this is not what I'm looking for. The problem is that I already generated the plot with data labels, and all I have access to now is the figure. Anything that requires regenerating the plot is not feasible for the actual data I'm working with.
If it turns out that Matlab doesn't already have some built-in feature for accessing all the data labels for a given plot at once, then of course I will have to regenerate the plot or use different software. But I would like to try to salvage what I have first.
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.
Well, it takes a bit of back-and-forth to clarify things, right? ;)
I don't want to call 'plot' or 'text' again-- I assume that calling 'text' is a part of the plot generation, because in the end, all I have is a .fig file with the data labels already assigned. Your solution requires me to call 'text' again, so in my view, it requires me to regenerate the plot. Does that make sense?
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)?
Yes. That’s a frequent problem here on Answers.
It's not a problem as a much as a part of the process when you agree to help someone. ;)
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.
Please see the updated question. My issue is not how to use text options; it is that I cannot call text again. One should assume that the data labels are given a priori.
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.
Unfortunately, you are solving a problem different than the one I have. The restriction on my situation is that the text labels are generated in a loop, as part of a bigger algorithm. Generating them again is too costly. I wanted to a solution that would allow me to reposition them as a group, the same way you can edit the properties of a line, if you are only given a .fig file.
After testing many options on my own, I've concluded that what I want to do is not possible with data labels. Perhaps it will be in future versions.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by