Avoid lower case in legend by plotting legend with 'DisplayName'

조회 수: 21 (최근 30일)
Nik Rocky
Nik Rocky 2020년 7월 3일
댓글: Nik Rocky 2020년 7월 6일
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
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
Image Analyst
Image Analyst 2020년 7월 4일
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?
Nik Rocky
Nik Rocky 2020년 7월 6일
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
madhan ravi 2020년 7월 3일
Use labels without _ .
regexprep('001_M1_Distance_0.5m_AKG_C1000S_F1_MS1','_','') % to remove underscores
  댓글 수: 2
Nik Rocky
Nik Rocky 2020년 7월 3일
It is no another way? I want have undercase delimiters =)
Nik Rocky
Nik Rocky 2020년 7월 3일
Sorry I was scribing in description wrong - I want keep '_' this, but I dont want to have a letters undercase!

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by