MATLAB Answers

Legend and graph doesn't have the same color

125 views (last 30 days)
Björn on 7 Aug 2014
Commented: aerothermal on 31 Aug 2016
I have a problem with the color in the legend. I'm using a package "RF Utilities V1.2" which I found at 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;
files = cell(1,2);
%Pre-allocate empty matrices:
S_parameter_matrix=zeros(number_of_selected_files, 4,...
freq=zeros(number_of_selected_files, number_of_measurement_points);
%%----------------------- THIS WAY WORKS ---------------------
%%----------------- 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))));
freq=freq/(1e-6); % GHz
%%---------------------- REST OF SCRIPT ----------------------
IL=-20*log10(abs(S21)); %Insertion loss
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,:))
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

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Aug 2014
Björn - I was able to reproduce the problem with your code (very helpful to post all that). The problem occurs during the plotting, at the line of code
plot(freq, IL(k,:))
IL is a 2x201 matrix, which is fine since k varies from 1 to 2. But, freq is a 2x201 matrix as well! So we are actually plotting the same data twice, and so are using four of the colours (as the second plot from each iteration just overlays the previous one).
Just replace the above line with
plot(freq(k,:), IL(k,:));
and it should work fine.
  1 Comment
Björn on 8 Aug 2014
Nice catch. Thank you very much!
It's pretty obvious now when you've pointed it out :-)

Sign in to comment.

More Answers (1)

Chad Greene
Chad Greene on 7 Aug 2014
You can link labels directly to their data with label. Labels take on the color of the data by default.
  1 Comment
Björn on 8 Aug 2014
Thank's for the tip! I will check it out.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!