Issues in legend color lines in for loop
1 次查看(过去 30 天)
显示 更早的评论
Hi,
Each iteration I am plotting the file has many arrays but legend granted a single line with the end of the file name otherwise same color for all the iteration files.
I made a color array based on my iteration and same for the legend
the attached graph will show whats the exact problem in legend
%% Start
clear all; close all; clc;
%%
D = uigetdir(pwd,'Select Directory');
S = dir(fullfile(D,'*.csv'));
file_list_1 = dir(fullfile(D, '*Peak + Bottom*.csv'));
CM = jet(length(file_list_1)/2); %for colouring
CM2=kron(CM,ones(2,1));%for duplicate the same colour in next next rows
%% Get the legend names from file list and remove the .csv and remove unwanted strings
legendTitle = cell(1,length(file_list_1));
for i = 1:length(file_list_1)
file=file_list_1(i).name;
%Your other operations
[~,fileName,~] = fileparts(file); % e.g., file is 'dp600_2_layers_L50.xlsx'
legendTitle{1,i} = fileName;
end
Legend_Modified=cellfun(@(x) x(20:min(end, 30)), legendTitle, 'UniformOutput', false); %remove unwanted strings
Legend_Modified=Legend_Modified';
%% Plot the results
for i=1:length(file_list_1)
file_name=file_list_1(i).name;
[x,y]=folder2array_V2(D, file_name);
xnew=ones(size(y)).*x;
xprime=xnew';
yprime=y';
plot(xprime,yprime,'--','LineWidth',2,'color',CM2(i,:)); hold on % plot the each file's bulk array
end
legend(Legend_Modified);
N_units=size(y,1);
3 个评论
dpb
2019-7-22
Yeah, but inside each of those there are at least three separate lines and so the twelve labels aren't actually being associated with the lines with the specific colors...in other words, the 12 lines you're actually labelling aren't the 12 that you need to label.
You'll need to save the handles of the actual lines drawn with the desired marker to call legend() with that array of line handles to associate the name.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!