Legend Colors in Loops
Show older comments
I am trying to have my legend colors correspond to my color-map. If you look at the last section of the code you can see where I have legend, but it is graphing the legend with points and then lines. I need the legend to correspond to the 2 different colors.
Here is what I have:
clear all close all clc
for e = 1:2 %% comparison of multiple Monte Carlo data sets
directory{e} = uigetdir;
files = dir(fullfile(directory{e},'*.txt')); % pick directory for the trial you would like to process
% This loop converts the fortran .txt output into workable MATLAB values.
for i = 1:length(files)
clear data_mat
fid = fopen(fullfile(directory{e},files(i).name),'r');
a{e} = textscan(fid,'%s %s %s');
fclose(fid);
for j = 1:3
temp = strrep(a{e}{j},'D','E');
data_mat(:,j) = str2double(temp);
end
[~,name,~] = fileparts(files(i).name);
datasets{e}.(name) = data_mat;
end
end
%% Survival Comparison (plot figure 1)
figure for i = [1 2 5 10 20 30 40 50] cmap = hsv(e) hold on
% calls the converted data sets for each different energy
for d = 1:e
b{d} = datasets{d}.(sprintf('agedData%dkeV',i));
end
% gathers the survival and standard deviation for the survival
for d = 1:e
survival_aged{d} = b{d}(1,2);
survival_ageddev{d} = b{d}(1,3);
end
% plots the survival
for d = 1:e
hold on
plot(i,survival_aged{d},'.','Color',cmap(d,:))
% adds 2xStdDeviation errorbars to the plot
errorbar(i,survival_aged{d},2*survival_ageddev{d},'.','Color',cmap(d,:))
%f{d} = directory{d}(end-25:end);
title('Damage Survival Comparison')
xlabel('Energy (keV)')
ylabel('Survival Fraction')
hold off
end
end
%% figure for i = 20 % define the data set that is used in the partition graph cmap = hsv(e) hold all
% calls the converted data sets for each different energy
for d = 1:e
b{d} = datasets{d}.(sprintf('agedData%dkeV',i));
clusterSizeAged{d} = b{d}(2:end,1);
partitionAged{d} = b{d}(2:end,2);
devAged{d} = b{d}(2:end,3);
plot(clusterSizeAged{d},partitionAged{d},'.','Color',cmap(d,:))
legend_titles{d} = sprintf('Dataset %s',directory{d}(end-33:end));
errorbar(clusterSizeAged{d},partitionAged{d},2*devAged{d},'Color',cmap(d,:))
end
legend(legend_titles);
title(sprintf('Comparison of Example Partition %dkeV',i))
xlabel('Cluster Size')
ylabel('Partition')
hold off
end
Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!