Edit data labels without regenerating plot

조회 수: 6 (최근 30일)
Kathryn Lund
Kathryn Lund 2019년 1월 15일
댓글: Star Strider 2019년 1월 17일
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개)

KSSV
KSSV 2019년 1월 15일
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
Kathryn Lund
Kathryn Lund 2019년 1월 15일
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.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2019년 1월 15일
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
Kathryn Lund
Kathryn Lund 2019년 1월 17일
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.
Star Strider
Star Strider 2019년 1월 17일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by