Legend and graph doesn't have the same color
Show older comments
I have a problem with the color in the legend. I'm using a package "RF Utilities V1.2" which I found at http://www.mathworks.com/matlabcentral/fileexchange/22996-rf-utilities-v1-2. I'm using the function citi2s from that package to import data from citi-files.
I have a pretty long script where I have a lot of functions, I just made a shorter version which will work to state the problem here. For instance I have only two lines to plot, which is easy to do without a for-loop. In my original script I have the number of plots will be determined by the number of files the user selects, and therefore I need the for loop. I also need to use hold all to change the color every time.
This is my code:
clear all;
pathname=char(strcat(pwd,'\'))
files = cell(1,2);
files{1,1}=['DATA01.D1'];
files{1,2}=['DATA02.D1'];
number_of_selected_files=length(files);
number_of_measurement_points=201;
%Pre-allocate empty matrices:
S_parameter_matrix=zeros(number_of_selected_files, 4,...
number_of_measurement_points(1));
freq=zeros(number_of_selected_files, number_of_measurement_points);
filename=zeros(number_of_selected_files,1);
%%----------------------- THIS WAY WORKS ---------------------
load('S21.mat')
number_of_selected_files=2;
freq=linspace(1,1.1,201)*10^9;
%%----------------- THIS WAY DOES NOT WORK -------------------
%{
for k=1:number_of_selected_files;
[S_parameter_matrix(k,1,:), S_parameter_matrix(k,2,:), ...
S_parameter_matrix(k,3,:), S_parameter_matrix(k,4,:), ...
freq(k,:)]=citi2s(char(strcat(pathname, files(k))));
end
freq=freq/(1e-6); % GHz
S21=squeeze(S_parameter_matrix(:,3,:));
%}
%%---------------------- REST OF SCRIPT ----------------------
IL=-20*log10(abs(S21)); %Insertion loss
fig1=figure;
hold all
for k=1:number_of_selected_files
[~, name, ext]=fileparts(files{k});
%Create filename-vector without file extension
filename_string{k} = [char(name)];
%Create string-vector with information for legend
legend_string{k}=['Cable ' num2str(k) ': ' char(name)];
plot(freq, IL(k,:))
end
hold off
leg=legend(legend_string, 'location', 'SouthOutside',....
'Interpreter', 'none');
My problem is that if I use the citi2s-function to import the data my plot uses a different colororder than my legend does. The legend follows the default color order but the plot doesn't. It looks like this:

If I just load the data from the S21.mat-file the color is fine, like this:

Does anyone know why the way of importing the data can affect the color-order in the plot? Or a way to fix it? I'm attaching a zip-file with the script, citi2s, data-files and the S21.mat
Thank you in advance
3 Comments
dpb
on 7 Aug 2014
What's the hold all there for before you've done anything to hold? We demonstrated in an earlier thread just this past week that setting hold state before an initial plot tends to muck things up. Remove it.
I believe then the primary problem w/ legend and colors is that you've then called hold off that resets the color order before legend. Remove it, too...
Then, legend should track the line patterns automagically. You can always ensure you're addressing the desired lines by saving and using the line handles and the optional handles array in legend but for simple case such as this shouldn't be necessary but it would look something like
hL=zeros(N,1); % N is loop max
for k=1:N
hL(k)=plot(freq, IL(k,:)); % save line handle
end
hLeg=legend(hL,legend_string, ...
Björn
on 8 Aug 2014
aerothermal
on 31 Aug 2016
Nice work!
Accepted Answer
More Answers (1)
Chad Greene
on 7 Aug 2014
0 votes
You can link labels directly to their data with label. Labels take on the color of the data by default.
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!