Conflict between "TextLocation" and "annotation" when using "Legend"

조회 수: 3 (최근 30일)
Sim
Sim 2022년 2월 9일
편집: Sim 2022년 2월 9일
Hi, I would like to use the position retrieved with the function TextLocation (by MathWorks Support Team) and use it for the annotation position.
However, when I do so the legend text in the subplot (2,2) takes the text of the annotation. How to solve this thing?
% input
x = -10:10;
city = {'London', 'Paris', 'Berlin','Madrid'};
season = {'summer','winter'};
colors = {'blue','red','black','magenta'};
% my plot (using "subaxis")
figure
for i = 1 : 4
subaxis(2,2,i);
y1 = (rand(1,21)-0.5)*20;
y2 = (rand(1,21)-0.5)*10;
hold on
p1(i) = plot(x, y1, 'Color', colors{i}, 'LineStyle','--');
p2(i) = plot(x, y2, 'Color', colors{i}, 'LineStyle','-');
hold off
p1(i).DisplayName = [city{i} ', ' season{1}];
p2(i).DisplayName = [city{i} ', ' season{2}];
legend % <-- "legend" here to see the legend text in the subplots
end
% annotation
str1 = 'further information';
str2 = sprintf('annotation: %s', str1);
% use the "TextLocation" position for "annotation"
mytext = TextLocation(str2,'Location','southeast');
dim = mytext.Position;
delete(mytext)
annotation('textbox',dim,'String',str2,'FitBoxToText','on');
% again "legend" here below to see the legend text in the subplot (2,2)
% since the legend text disappears in that subplot when using "TextLocation"
legend

채택된 답변

DGM
DGM 2022년 2월 9일
편집: DGM 2022년 2월 9일
Oof I didn't notice what was going on. When TextLocation.m creates the temporary legend object to scrape geometry from, it sets the DisplayName property of the first plot object in the axes. So you have to run TextLocation before creating the legend and before setting the DisplayName properties.
% input
x = -10:10;
city = {'London', 'Paris', 'Berlin','Madrid'};
season = {'summer','winter'};
colors = {'blue','red','black','magenta'};
% my plot (using "subaxis")
for i = 1 : 4
subaxis(2,2,i);
y1 = (rand(1,21)-0.5)*20;
y2 = (rand(1,21)-0.5)*10;
hold on
p1(i) = plot(x, y1, 'Color', colors{i}, 'LineStyle','--');
p2(i) = plot(x, y2, 'Color', colors{i}, 'LineStyle','-');
hold off
p1(i).DisplayName = [city{i} ', ' season{1}];
p2(i).DisplayName = [city{i} ', ' season{2}];
legend % <-- "legend" here to see the legend text in the subplots
end
% annotation
str1 = 'further information';
str2 = sprintf('annotation: %s', str1);
mytext = TextLocation(str2,'Location','southeast');
set(mytext,'String',str2,'FitBoxToText','on','linestyle','-');
p1(i).DisplayName = [city{i} ', ' season{1}]; % fix the DisplayName
legend
Either that or conditionally create the annotation inside the loop before the last legend is created.
It's not part of the problem, but note that I made this simplification:
Instead of doing this unnecessary duplication:
mytext = TextLocation(str2,'Location','southeast'); % < -- this is a textbox annotation
dim = mytext.Position;
delete(mytext) % you delete it
annotation('textbox',dim,'String',str2,'FitBoxToText','on'); % and then remake it again
Just set the properties of the annotation you already have.
mytext = TextLocation(str2,'Location','southeast');
set(mytext,'String',str2,'FitBoxToText','on','linestyle','-');
Either that, or you could just open TextLocation and tailor its default behavior to your tastes.
  댓글 수: 1
Sim
Sim 2022년 2월 9일
편집: Sim 2022년 2월 9일
Many thanks @DGM for having solved my messy code!
I am going to use your simplification!
:-)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by