Plot multiple legend in a loop with variable

Hello
In result folder are multiple subfolder with .mat-files (in this example 3)
I want load and print all .mat-files in one plot
Legend should show a .mat-filenames
I tried to make different dancing moves, but it still shows me just one (last filename) and data1 (that I want to remove)
Legends_results =
1×3 cell array
{'001_M1_0.5m_AKG_F1_MS1.mat'} {'002_M1_0.5m_AKG_F1_MS1.mat'} {'003_M1_0.5m_AKG_F1_MS1.mat'}
Help me please to:
  1. plot all names to legend
  2. remove/avoid plot "data1"
my code:
steps_SNR = 1; %%%change
addpath(genpath('/home/nikitajarocky/workspace/QT/Software_2.0_QT/IO/'));
i = dir('**/*.mat');
Legends_results = cell(1,length(i));
for p = 1:length(i)
roc_file_name = i(p).name;
load(roc_file_name)
disp(roc_file_name)
for a = 1:length(i)
Legends_results{a}=i(a).name;
f=@(m) repmat(c,1,nnz(Legends_results));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SNR_help = ((length(M)-1)/2)*steps_SNR;
SNR = -SNR_help:steps_SNR:SNR_help;
%figure('Name',' receiver operating characteristic','NumberTitle','on');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold all
p1 = plot(M(:,2),M(:,1),'--o');
buffer = [.1 .3 .5];
buffer = repmat(buffer,1,ceil(numel(M(:,2))/numel(buffer)));
buffer(numel(M(:,2))+1:end) = [];
[~, ySortIdx] = sort(M(:,2));
buffer(ySortIdx) = buffer;
labelpoints(M(:,2),M(:,1), SNR, 'E', buffer)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold all
lgd = legend([p1],Legends_results{p},'Interpreter','none','Location','Best');
title(lgd,'Compare AKG and Sony')
hold all
help_x = 0:0.1:1;
help_y = 0:0.1:1;
plot(help_x,help_y,'--','Color','g');
xlabel('False discovery rate')
ylabel('True positive rate')
xlim([0 1]);
ylim([0 1]);
set(get(get(gco,'Annotation'),'LegendInformation'),'IconDisplayStyle','off')
end
Thank you in advice!

4 Comments

I found somethink, with:
h = plot(help_x,help_y,'--','Color','g');
h.Annotation.LegendInformation.IconDisplayStyle = 'off';
i have disabled 'data1' legend entry
with:
legend(Legends_results,'Location','northeast')
I'm plotting Legends_results cell array with names, but there are no connection to plots, so dot-colors are independent of legend
code
steps_SNR = 1; %%%input
addpath(genpath('/home/nikitajarocky/workspace/QT/Software_2.0_QT/IO/'));
i = dir('**/*.mat');
Legends_results = cell(1,length(i));
for j = 1:length(i)
roc_file_name = i(j).name;
load(roc_file_name)
disp(roc_file_name)
for a = 1:length(i)
Legends_results{a}=i(a).name;
f=@(m) repmat(c,1,nnz(Legends_results));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SNR_help = ((length(M)-1)/2)*steps_SNR;
SNR = -SNR_help:steps_SNR:SNR_help;
%figure('Name',' receiver operating characteristic','NumberTitle','on');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold all
p = plot(M(:,2),M(:,1),'--o');
buffer = [.1 .3 .5];
buffer = repmat(buffer,1,ceil(numel(M(:,2))/numel(buffer)));
buffer(numel(M(:,2))+1:end) = [];
[~, ySortIdx] = sort(M(:,2));
buffer(ySortIdx) = buffer;
labelpoints(M(:,2),M(:,1), SNR, 'E', buffer)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold all
title(lgd,'Compare AKG and Sony')
hold all
legend(Legends_results,'Location','northeast')
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';
xlabel('False discovery rate')
ylabel('True positive rate')
xlim([0 1]);
ylim([0 1]);
end
I moving forward,
The colors should be right chosen, of course of failed connection between plot and legend.
It is possible to choose a color order for plot and legend? So, if I have 3 files for plot and legend, it should be a same color order!?
  • It is possible to choose a color order for plot and legend? So, if I have 3 files for plot and legend, it should be a same color order!?
It's the same color order for sure. To check plot order to check color order
Hey darova,
not really. I was forced to use:
ColorCell{1} = hex2rgb('#0072BD');
ColorCell{2} = hex2rgb('#EDB120');
ColorCell{3} = hex2rgb('#7E2F8E');
hold on
p = plot(M(:,2),M(:,1),'--o','Color',ColorCell{j});

Sign in to comment.

 Accepted Answer

I suggest you apply this 2-step approach to your code.
Step 1: Use the DisplayName property of graphics objects to define the legend strings.
Step 2: Use the graphics object handles to specify what is included in the legend.
This example below shows the structure of these two steps.
cla() % clear axes
hold on
n = 6;
h = gobjects(n,3); % used to store the graphics handles
labels = {'aaa' 'bbb' 'ccc' 'ddd' 'eee' 'fff'};
colors = lines(n);
for j = 1:3
for i = 1:n
% STEP 1: use DisplayName
h(i,j) = plot(1:5, rand(1,5)+i-1, '-o', 'Color', colors(i,:), 'DisplayName', labels{i})
end
end
% STEP 2: Specify which handles are included.
legend(h(:,1)); % Only include the 1st column of handles

7 Comments

Good morning Adam!
You are avesome! I change my whole code and now it works phantastic! Tahnk you for your help!
steps_SNR = 5; %%% give step size and be happy
save_plot = 1; %%% print
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cla()
addpath(genpath('/home/user/workspace/QT/Software_2.0_QT/IO/'));
i = dir('**/*.mat');
name = cell(1:length(i));
for j = 1:length(i)
roc_file_name = i(j).name;
load(roc_file_name)
disp(roc_file_name)
folder = i(j).folder;
name = {sprintf('%s_vs_',labels{:})};
labels = extractBefore({i.name}, ".mat");
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SNR_help = ((length(M)-1)/2)*steps_SNR;
SNR = -SNR_help:steps_SNR:SNR_help;
%figure('Name',' receiver operating characteristic','NumberTitle','on');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold on
colors = lines(j);
p = plot(M(:,2),M(:,1),'-o', 'Color', colors(j,:),'DisplayName',labels{j});
buffer = [.1 .3 .5];
buffer = repmat(buffer,1,ceil(numel(M(:,2))/numel(buffer)));
buffer(numel(M(:,2))+1:end) = [];
[~, ySortIdx] = sort(M(:,2));
buffer(ySortIdx) = buffer;
labelpoints(M(:,2),M(:,1), SNR, 'E', buffer)
lgd = legend(labels,'Interpreter','none');
title(lgd,'Compare AKG and Sony')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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';
xlabel('False discovery rate')
ylabel('True positive rate')
xlim([0 1]);
ylim([0 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
name = string(name);
file_save = {sprintf(labels{:},'.png')}; fullfile([name,'.png']);
if save_plot == 1
export_fig(name)
end
end
Glad I could help!
Since the legend labels are assigned using DisplayName, you don't need to include the labels in the call to legend().
lgd = legend('Interpreter','none');
Thank you! but now I have another Problem. If I want to evaluate directly 10+ results, I get:
Error using cell
Requested 1x2x3x4x5x6x7x8x9x10x11x12x13x14x15 (9742.9GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and
cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
Error in Plot_ROC_26_06_20 (line 12)
name = cell(1:length(i));
How ca I change it? I mean in every cell are just 9x2 doubles. Maybe I have to convert cells in matrix?
name = cell(1:length(i)); %should be removed, than it works great
Try cell(1, numel(i))
I have the same problem but it is a bit more complex. Any idea on how this can be performed on variable names within a table?
Adam Danz
Adam Danz on 11 Jan 2022
Edited: Adam Danz on 11 Jan 2022
Instead of using labels={...} you'll use T.Properties.VariableNames where T is your table, assuming the columns of the table were plotted in order.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 25 Jun 2020

Edited:

on 11 Jan 2022

Community Treasure Hunt

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

Start Hunting!