Avoid lower case in legend by plotting legend with 'DisplayName'
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
I have again a similar Problem, but in this case I can't separate legend, and : 'Interpreter','none' is here also not working. How can I print names like 001_M1_Distance_0.5m_AKG_C1000S_F1_MS1 without undercases?
label1 = extractBefore({i(j).name}, ".mat");
label1 = extractAfter(label1, "_");
...........
if any(M1)
plot(M1(:,2),M1(:,1),'-o','DisplayName',label1{1});
hold on
end
if any(M2)
plot(M2(:,2),M2(:,1),'-o','DisplayName',label2{1});
hold on
end
if any(M3)
plot(M3(:,2),M3(:,1),'-o','DisplayName',label3{1});
hold on
end
if any(M4)
plot(M4(:,2),M4(:,1),'-o','DisplayName',label4{1});
hold on
end

Thanks!
채택된 답변
Image Analyst
2020년 7월 3일
Use the 'Interpreter', 'none' option in legend():
% Read headers
plot(1:10);
hold on
plot(4:14);
legend('plot_1', 'plot_2', 'Interpreter', 'none', 'Location', 'Northwest');

댓글 수: 6
Its not possible, because there are different combination of labels (see my code). Some times that is label 1 an 2/ sometimes 3 and 4....
Set the DisplayName property of the lines. Store the handles of those lines in a vector. Pass the subset of the vector of handles that you want to appear in the legend into the legend function and specify the name-value pair 'Interpreter', 'none' after that vector.
x = 0:360;
h = gobjects(1, 5);
axis([0 360 -1 1]);
hold on
for k = 1:5
h(k) = plot(x, sind(k*x), 'DisplayName', "sine_" + k);
end
legend(h([1 3 4]), 'Interpreter', 'none')
I don't know why you say 'Interpreter', ''none' doesn't work when Steve and I both say it does, and we prove it. This also works (slightly different than Steve) and doesn't matter if you have "different combination of labels"
x = 0:360;
h = gobjects(1, 5);
axis([0 360 -1 1]);
hold on
for k = 1:5
plot(x, sind(k*x));
legendStrings{k} = sprintf('sine_%d', k);
end
legend(legendStrings, 'Interpreter', 'none')
You say "see my code". I did and it works fine as far as I can see:
filename = '001_M1_Distance_0.5m_AKG_C1000S_F1_MS1.mat';
label1 = extractBefore(filename, ".mat")
label1 = {extractAfter(label1, "_")}
label2 = label1
label3 = label1;
label4 = label1;
% Create sample data.
M1 = rand(10, 2);
M2 = rand(10, 2);
M3 = rand(10, 2);
M4 = rand(10, 2);
M1 = sortrows(M1, 1);
M2 = sortrows(M2, 1);
M3 = sortrows(M3, 1);
M4 = sortrows(M4, 1);
plotCount = 1;
legendStrings = [];
if any(M1)
plot(M1(:,2),M1(:,1),'-o','DisplayName',label1{1});
legendStrings{plotCount} = label1{1};
plotCount = plotCount + 1;
hold on
end
if any(M2)
plot(M2(:,2),M2(:,1),'-o','DisplayName',label2{1});
legendStrings{plotCount} = label2{1};
plotCount = plotCount + 1;
hold on
end
if any(M3)
plot(M3(:,2),M3(:,1),'-o','DisplayName',label3{1});
legendStrings{plotCount} = label3{1};
plotCount = plotCount + 1;
hold on
end
if any(M4)
plot(M4(:,2),M4(:,1),'-o','DisplayName',label4{1});
legendStrings{plotCount} = label4{1};
plotCount = plotCount + 1;
hold on
end
legend(legendStrings, 'Interpreter', 'none');

So you say it doesn't work on your computer, so show us your exact code so we can find out why 'Interpreter' 'none' is not working for you alone. Attach screenshot along with the code so we can see how it failed. Then copy and run the code above and tell me if that works.
Wow, Steven Lord and Image Analyst — it works now, thank you very much! I think i had some wrong line inside (it's a big code), so now I make a completely new part and its works! Your help is amazing, you are amazing! =)
Maybe additional question (to avoid opening net topic), that is easy for you, I think. You see, I make 4 different cases with plot.
Actually I have multiple files with names:
003_M1_Distance_0.5m_AKG_C1000S_F1_MS1.mat
003_M1_Distance_2m_AKG_C1000S_F1_MS1.mat
001_M1_Distance_0.5m_AKG_C1000S_F1_MS1.mat
003_M1_Distance_0.5m_SONY_PCM_D50_F1_MS1.mat
001_M1_Distance_2m_AKG_C1000S_F1_MS1.mat
003_M1_Distance_2m_AKG_C1000S_F1_MS1.mat
001_M1_Distance_2m_SONY_PCM_D50_F1_MS1.mat
003_M1_Distance_2m_SONY_PCM_D50_F1_MS1.mat
003_M1_Distance_0.5m_SONY_PCM_D50_F1_MS1.mat
002_M1_Distance_2m_SONY_PCM_D50_F1_MS1.mat
003_M1_Distance_2m_SONY_PCM_D50_F1_MS1.mat
006_M2_Distance_0.5m_AKG_C1000S_F1_MS1.mat
.......... 100 x more
and I habe to check loaded files for namepart (groups) with '0.5m', '2m', 'AKG' or 'SONY'.
There are allways combination of two groups and I plot later just twis groups (exmpl: const 0.5m and SONY vs AKG; const SONY and 0.5m vs 2m):
if (strcmp(str_dist,'0.5m')|| strcmp(str_dist,'0.5')) && strcmp(str_mic,'AKG')
R1 = R1 + M;
cnt1 = cnt1 + 1;
label1 = extractBefore({i(j).name}, ".mat");
label1 = extractAfter(label1, "_");
M1 = (R1/cnt1);
elseif strcmp(str_dist,'2m') && strcmp(str_mic,'AKG')
R2 = R2 + M;
cnt2 = cnt2 + 1; % I know, one cnt is enough
label2 = extractBefore({i(j).name},".mat");
label2 = extractAfter(label2, "_");
M2 = (R2/cnt2);
elseif (strcmp(str_dist,'0.5m')|| strcmp(str_dist,'0.5')) && strcmp(str_mic,'SONY')
R3 = R3 + M;
cnt3 = cnt3 + 1;
label3 = extractBefore({i(j).name}, ".mat");
label3 = extractAfter(label3, "_");
M3 = (R3/cnt3);
elseif strcmp(str_dist,'2m') && strcmp(str_mic,'SONY')
R4 = R4 + M;
cnt4 = cnt4 + 1; % I know, one cnt is enough
label4 = extractBefore({i(j).name}, ".mat");
label4 = extractAfter(label4, "_");
M4 = (R4/cnt4);
end
Its looks not ellegant, did you have an idea? Thank you very very much!
I'd use counter instead of cnt - it's more descriptive it sounds a lot less naughty, than "I know, one cnt is enough".
You might want to consider contains(string, pattern, 'IgnoreCase', true) instead of strcmp(). Or at least use strcmpi() for more robustness. And you might want to cast to lower because you're never totally sure if the extension returned by the operating system will be upper case or lower case:
extractBefore(lower({i(j).name}), '.mat');
When you're assigning the M's you don't need parentheses:
M4 = R4 / cnt4;
Personally I like spaces around operators but that's a matter of style.
You should also have an else clause with no if. What if none of the criteria are satisfied? Will your code will work, or will you get an error downstream because nothing got assigned?
Hello Image Analyst! Thank you very much!
I solve all of you tipps:
clear
steps_SNR = 15; %%% give step size and be happy
save_plot = 0; %%% print
print_lablepoints = 1; %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TIME1 = datestr(now, 'dd/mm/yy-HH:MM:SS');
disp('*******************************************************************')
disp('Beginn:')
disp(TIME1)
disp('*******************************************************************')
addpath(genpath('/MATLAB Drive/06.07.20/'));
%Res = ('/home/user/workspace/QT/Software_2.0_QT/SORT_RESULTS/End/');
%ah = cd('/home/user/workspace/QT/Software_2.0_QT/IO/RESULTS/VS/');
Res = ('/MATLAB Drive/06.07.20/End');
set(groot, 'DefaultFigureVisible', 'on')
i = dir('**/*.mat');
tic
%cla()
M_input_1 = cell(length(i), 1);
M_input_2 = cell(length(i), 1);
M_input_3 = cell(length(i), 1);
M_input_4 = cell(length(i), 1);
plotCount = 1;
legendStrings = [];
Value_Sort = cell(length(i),1);
for j = 1:length(i)
roc_file_folder = i(j).folder;
roc_file_name = i(j).name;
ROC_File_Folder_Name = fullfile(roc_file_folder,roc_file_name);
load(ROC_File_Folder_Name)
disp(roc_file_name)
folder = i(j).folder;
Title_Parts = strsplit(folder, '/');
Motorentyp = Title_Parts{1,end-3};
Motorentyp = strsplit(Motorentyp, '_');
Motorentyp = Motorentyp{1,end};
Mikrofon_Distanz_1 = Title_Parts{1,end-1};
Mikrofon_Distanz_2 = Title_Parts{1,end};
Motoregruppe = Title_Parts{1,end-2};
Motoregruppe = strsplit(Motoregruppe, '_');
Motoregruppe = Motoregruppe{1,end};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SNR_help = ((length(M)-1)/2)*steps_SNR;
SNR = -SNR_help:steps_SNR:SNR_help;
%figure('Name',' receiver operating characteristic','NumberTitle','on');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
str = strsplit(roc_file_name, '_');
str_dist = str{1,4};
str_mic = str{1,5};
if (strcmpi(str_dist,'0.5m')|| strcmpi(str_dist,'0.5')) && strcmpi(str_mic,'AKG')
M_input_1{j} = M;
%label1 = strjoin({'Motorentyp:',Motorentyp,',Motorengruppe:',Motoregruppe,',Setup:',Mikrofon_Distanz1,'und',Mikrofon_Distanz2});
label1 = strjoin({'Setup:',Mikrofon_Distanz_1,'und',Mikrofon_Distanz_2});
elseif strcmpi(str_dist,'2m') && strcmpi(str_mic,'AKG')
M_input_2{j} = M;
%label2 = strjoin({'Motorentyp:',Motorentyp,',Motorengruppe:',Motoregruppe,',Setup:',Mikrofon_Distanz1,'und',Mikrofon_Distanz2});
label2 = strjoin({'Setup:',Mikrofon_Distanz_1,'und',Mikrofon_Distanz_2});
elseif (strcmpi(str_dist,'0.5m')|| strcmpi(str_dist,'0.5')) && strcmp(str_mic,'SONY')
M_input_3{j} = M;
label3 = strjoin({'Setup:',Mikrofon_Distanz_1,'und',Mikrofon_Distanz_2});
%label3 = strjoin({'Motorentyp:',Motorentyp,',Motorengruppe:',Motoregruppe,',Setup:',Mikrofon_Distanz1,'und',Mikrofon_Distanz2});
elseif strcmpi(str_dist,'2m') && strcmpi(str_mic,'SONY')
M_input_4{j} = M;
label4 = strjoin({'Setup:',Mikrofon_Distanz_1,'und',Mikrofon_Distanz_2});
%label4 = strjoin({'Motorentyp:',Motorentyp,',Motorengruppe:',Motoregruppe,',Setup:',Mikrofon_Distanz1,'und',Mikrofon_Distanz2});
else
disp('Selfdestroy activated')
end
for u = 1:length(M)
if print_lablepoints ==1
buffer = [.2 .3 .4];
buffer = buffer * 2;
buffer = repmat(buffer,1,ceil(numel(M(:,2))/numel(buffer)));
buffer(numel(M(:,2))+1:end) = [];
[~, ySortIdx] = sort(M(:,2));
buffer(ySortIdx) = buffer;
end
end
end
SNR = compose('%d dB', SNR);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
M_input_1 = cat(3, M_input_1{:});
M1 = mean(M_input_1, 3);
S1 = std(M_input_1,0,3);
M_input_2 = cat(3, M_input_2{:});
M2 = mean(M_input_2, 3);
S2 = std(M_input_2,0,3);
M_input_3 = cat(3, M_input_3{:});
M3 = mean(M_input_3, 3);
S3 = std(M_input_3,0,3);
M_input_4 = cat(3, M_input_4{:});
M4 = mean(M_input_4, 3);
S4 = std(M_input_4,0,3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax = gca;
ax.SortMethod='ChildOrder';
if any(M1)
%plot(M1(:,2),M1(:,1),'-o','DisplayName',label1{1});
e1 = errorbar(M1(:,2),M1(:,1),S1(:,2),S1(:,2),S1(:,1),S1(:,1),'--o','LineWidth',2,'MarkerFaceColor','cyan','MarkerSize',10,'MarkerEdgeColor','black','DisplayName',label1);
e1.LineWidth = 0.5;
legendStrings{plotCount} = label1;
plotCount = plotCount + 1;
a = labelpoints(M1(:,2),M1(:,1), SNR, 'E',buffer,'FontSize', 12);
set(a, 'interpreter', 'latex');
hold on
end
if any(M2)
%plot(M2(:,2),M2(:,1),'-o','DisplayName',label2{1});
e2 = errorbar(M2(:,2),M2(:,1),S2(:,2),S2(:,2),S2(:,1),S2(:,1),'--s','LineWidth',2,'MarkerFaceColor','yellow','MarkerSize',10,'MarkerEdgeColor','black','DisplayName',label2);
e2.LineWidth = 0.5;
legendStrings{plotCount} = label2;
plotCount = plotCount + 1;
b = labelpoints(M2(:,2),M2(:,1), SNR, 'E', buffer,'FontSize', 12);
set(b, 'interpreter', 'latex');
hold on
end
if any(M3)
%plot(M3(:,2),M3(:,1),'-o','DisplayName',label3{1});
e3 = errorbar(M3(:,2),M3(:,1),S3(:,2),S3(:,2),S3(:,1),S3(:,1),'--d','LineWidth',2,'MarkerFaceColor','magenta','MarkerSize',10,'MarkerEdgeColor','black','DisplayName',label3);
e3.LineWidth = 0.5;
legendStrings{plotCount} = label3;
plotCount = plotCount + 1;
c = labelpoints(M3(:,2),M3(:,1), SNR, 'E', buffer,'FontSize', 12);
set(c, 'interpreter', 'latex');
hold on
end
if any(M4)
%plot(M4(:,2),M4(:,1),'-o','DisplayName',label4{1});
e4 = errorbar(M4(:,2),M4(:,1),S4(:,2),S4(:,2),S4(:,1),S4(:,1),'--v','LineWidth',2,'MarkerFaceColor','green','MarkerSize',10,'MarkerEdgeColor','black','DisplayName',label4);
e4.LineWidth = 0.5;
legendStrings{plotCount} = label4;
plotCount = plotCount + 1;
d = labelpoints(M4(:,2),M4(:,1), SNR, 'E', buffer,'FontSize', 12);
set(d, 'interpreter', 'latex');
hold on
end
lgd = legend(legendStrings, 'Interpreter', 'none','Location','northeast');
title(lgd,'Vergleich zwischen Drehzahlsetups:')
%title(['ROC: Motorentyp ',Motorentyp,', Motorengruppe ',Motoregruppe,', Konstante: ',Mikrofon_Distanz_1],'Interpreter','none')
title(['ROC: Motorentyp ',Motorentyp,', Motorengruppe ',Motoregruppe],'Interpreter','none')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
help_x = 0:0.1:1;
help_y = 0:0.1:1;
h = plot(help_x,help_y,'--','Color','g');
h.Annotation.LegendInformation.IconDisplayStyle = 'off';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if exist('a','var'); uistack(a,'top'); end
if exist('b','var'); uistack(b,'top'); end
if exist('c','var'); uistack(c,'top'); end
if exist('d','var'); uistack(d,'top'); end
xlabel('False discovery rate')
ylabel('True positive rate')
axis equal
xlim([0 1]);
ylim([0 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%set(0,'DefaultFigureColor','remove')
set(0,'DefaultFigureColor',[1 1 1])
file_save = strcat('Motorentyp_',Motorentyp,'_Motorengruppe_',Motoregruppe,'_Konstante_',Mikrofon_Distanz_1,'.pdf');
Full_Export_Path = fullfile(Res,file_save);
if save_plot == 1
export_fig(Full_Export_Path)
%save(Full_Export_Path)
end
disp('*******************************************************************')
disp('Time Beginn:')
disp(TIME1)
disp('*******************************************************************')
disp('Time finish:')
TIME2 = datestr(now, 'dd/mm/yy-HH:MM:SS');
disp(TIME2)
disp('*******************************************************************')
toc
Thank you!
추가 답변 (1개)
madhan ravi
2020년 7월 3일
Use labels without _ .
regexprep('001_M1_Distance_0.5m_AKG_C1000S_F1_MS1','_','') % to remove underscores
댓글 수: 2
It is no another way? I want have undercase delimiters =)
Sorry I was scribing in description wrong - I want keep '_' this, but I dont want to have a letters undercase!
카테고리
도움말 센터 및 File Exchange에서 Create Plots on Maps에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
