Set same color order for plots and legend

9 views (last 30 days)
Nik Rocky
Nik Rocky on 25 Jun 2020
Answered: Nik Rocky on 25 Jun 2020
Hello,
I have a problem to connect plot with legend. There are a variable number of plots and legends, so there are not fixed values.
Of course of this I can't use
p1 = plot(....
p2 = plot(....
legend([p1 p2],{'Test1','Test2'})
Me also take the filenames like legend entries.
The big problem — the colors of plots and legend are different (there are not connected with each other)
But I think about one workaround. Index of my files is still the same (here 1,2,3) so can I adjust to getting same colors in plots and legend?
(r, g, b)?
Thank you!

Answers (1)

Nik Rocky
Nik Rocky on 25 Jun 2020
I do a workarround but it works (first) just for 3 plots.
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));
ColorCell = cell(1,length(i));
for j = 1:length(i)
roc_file_name = i(j).name;
load(roc_file_name)
disp(roc_file_name)
ColorCell{1} = hex2rgb('#0072BD');
ColorCell{2} = hex2rgb('#EDB120');
ColorCell{3} = hex2rgb('#7E2F8E');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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','Color',ColorCell{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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
for a = 1:length(i)
Legends_results{a}=i(a).name;
Legends_results{a} = extractBefore(Legends_results{a}, ".mat");
f=@(m) repmat(c,1,nnz(Legends_results));
drawnow
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hold all
lgd = legend(Legends_results,'Interpreter','none','Location','northeast');
hold all
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]);
end

Community Treasure Hunt

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

Start Hunting!