Edit data labels without regenerating plot
조회 수: 6 (최근 30일)
이전 댓글 표시
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.)
댓글 수: 0
답변 (2개)
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) ;
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
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 Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!