if else in for loop+looping through line colors for plot

2 Ansichten (letzte 30 Tage)
Katherine
Katherine am 9 Dez. 2013
Kommentiert: Image Analyst am 10 Dez. 2013
I'm reading a set of files and setting my plotting characteristics. When I get to a certain number of variables, 15 in this case I run out of regular RGB colors, other than yellow and black. I am using another variable later on in my script as black and yellow is just hard to see. I'm trying to add in additional colors in my loop but I get the index exceeds matrix dimensions error.
This is due to the fact that i is at 16, but I only have 15 total additional Line styles and 5 additional colors. How do I get it to continue going through the files but just start at the beginning of the other LineStyles and extraLineColor variables to continue plotting other lines styles/colors?
Here's my code:
% % %load in files
[files]= dir('*.median.stat');
% %Set plotting characteristics
lWidth = 2;
extraLineColors={rgb('orange') rgb('purple') rgb('DarkGray') rgb('Pink') rgb('Brown')};
LineStyles = {'--' ':' '-' '--' ':' '-' '--' ':' '-' '--' ':' '-' '--' ':' '-'};
stationLineStyles = {'r--' 'r:' 'r-' 'b--' 'b:' 'b-' 'g--' 'g:' 'g-' 'm--' 'm:' 'm-' 'c--' 'c:' 'c-'};
labelSize = 14;
titleSize = 16;
if (i <= 15)
semilogx(files(i).data(:,1),files(i).data(:,2), stationLineStyles{i},'LineWidth',lWidth)
hold on
else (i > 15) semilogx(files(i).data(:,1),files(i).data(:,2),'LineStyle',LineStyles{i},'Color',
extraLineColors{i},'LineWidth',lWidth)
legendn{i} = [sta ' ' loc ' ' chan];
end
end
I know I am probably doing something really silly here or am just tired. Can anyone clue me into to what I'm doing wrong?

Akzeptierte Antwort

Image Analyst
Image Analyst am 9 Dez. 2013
Try using a predefined colormap like jet.
myColorMap = jet(length(files)); % One unique color for each file.
Then, to use this colormap for the ith line, do this:
semilogx(files(i).data(:,1),files(i).data(:,2),...
'LineStyle',LineStyles{i}, ...
'Color',myColorMap(i,:),...
'LineWidth',lWidth)
  8 Kommentare
Image Analyst
Image Analyst am 9 Dez. 2013
You could just pick random colors that are different than each other. Expand out by 3 to have every three curves be the same color (let me know if you can't figure out how to do that):
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
% Make 20 plots, with 25 data points in each plot.
numberOfDataSets = 20;
x = 1:25;
y = rand(numberOfDataSets, length(x));
% These y would all be on top of each other.
% Separate the plots vertically.
offsets = repmat((1:numberOfDataSets)', [1, length(x)]);
y = y + offsets;
% Make up a different colormap for each line
cm = rand(numberOfDataSets,3); % Initialize with random color.
% Replicate it 3 times
% cm = repmat(cm, [3, 1])
for p = 2 : numberOfDataSets
nextColor = rand(1,3); % Random color.
% Make sure it's not too bright (close to white).
if sum(nextColor) > 0.8*3
% It's too bright, pick another color.
continue;
end
% Make sure it's different than the last color by some amount
priorColor = cm(p-1,:);
distance = sqrt((priorColor(1) - nextColor(1))^2 + ...
(priorColor(2) - nextColor(2))^2 + ...
(priorColor(3)- nextColor(3))^2);
if distance > 0.3 % or whatever
cm(p, :) = nextColor;
end
end
% Now plot the curves.
for p = 1 : numberOfDataSets
thisColor = cm(p, :)
plot(x,y(p,:), 'Color', thisColor,'LineWidth', 3);
hold on;
end
caption = sprintf('%d plots with the Initial Default Color Order (Note the repeating colors)', numberOfDataSets);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Give a name to the title bar.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
msgbox('Done with demo!');
Image Analyst
Image Analyst am 10 Dez. 2013
If you want colors that are all different from each other by at least some specified amount, you can use this code:
% Script to pick widely different colors in RGB space and plot a curve with each one.
% Colors are guaranteed to have a color difference (in RGB space) of at least 0.1 (25.5 gray levels).
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
minColorSeparation = 0.1; % In RGB space. Colors will be no closer than this to each other.
r = 0 : minColorSeparation : 1;
g = r;
b = r;
% Make a 3D grid of all color coordinates possible.
[rr, gg, bb] = meshgrid(r, g, b);
% Now we have created all possible colors.
% Select a number of colors to extract from the total possible colors.
numberOfColors = 10;
randomColors = sort(randperm(numel(rr), numberOfColors))
% These are the indexes of the randomly chosen colors.
fprintf('Just chose %d colors from a possible pallette of %d colors.\n',...
numberOfColors, numel(rr));
% Make "numberOfColors" plots, with 25 data points in each plot.
x = 1:25;
y = rand(numberOfColors, length(x));
% These y would all be on top of each other.
% Separate the plots vertically.
offsets = repmat((1:numberOfColors)', [1, length(x)]);
y = y + offsets;
% Extract a subset of "numberOfColors" colors from the total possible 11x11x11 colors.
selectedColors = [rr(randomColors)', gg(randomColors)', bb(randomColors)'];
% Plot "numberOfColors" lines, one in each color.
for p = 1 : numberOfColors
% Get the index of this random color in the colormap.
thisIndex = randomColors(p);
% Get the RGB value from the arrays.
thisColor = [rr(thisIndex), gg(thisIndex), bb(thisIndex)];
plot(x, y(p,:), 'Color', thisColor, 'LineWidth', 3);
hold on;
end
caption = sprintf('%d plots with very different colors', numberOfColors);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
grid on;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Orange finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by