How can I automatically assign colors to data that's being dynamically plotted and recycle the colors if there's more data that needs to be plotted?

11 views (last 30 days)
I'm writing a script that plots data that's been read from a file. The plot is dynamic and what is actually plotted depends on the data that's in the file. The data appears as different numbered AOIs, and it specifically plots the rows and columns of each AOI. So row1 and col1 correspond to AOI_1. So it might plot data from any combination of AOIs with at most 8 different rows and columns of AOIs on the plot. I would like each of these rows and columns of the specific AOIs to be plotted using the following colors: red, green, blue, and cyan. The first row and col plotted will receive red; the second row and column plotted will be green; the third row and column plotted will be blue; etc. Once theses colors run out, in the event of more AOIs being plotted than colors available, I want to recycle the colors, as well as issue a warning that too many AOIs have been plotted for it to be a nice plot. (Note that AOI_1's row1 and col1 can have the same color and that they are differentiated by line type. So all rows are solid lines and all columns are dashed lines.) I'm wondering how to plot the first AOI's row and col with the first color, and the second AOi's row and col with the second number (where the first AOI is not necessarily AOI_1 and the second AOI is not necessarily AOI_2), and when all colors have been used, the the first color is again used for the next AOI info, while issuing a warning. Here's some sample code I have so far. Any help is appreciated.
AOI=[0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 8 8 8 8 8 8];
row1=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col1= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row2=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col2= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row3=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col3= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row4=[25 25 25 25 26 26.5 27 25 27 25 26 27 26 25 27 24 24 25 26 26 26 27 27 27 28 28 24 24 25 25 25 25 25 25 25 25 25 26 26.5 27 25 27 26 25 27 24 24 25 26 26 26 27 27 27 28 28 24 24 25 25 25 25 25];
col4= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row5=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col5= [66 67 67 68 66 67 66 66 66 67 65 65 66 66 66 64 65 65 66 66 67 67 68 68 66 66 65 66 65 66 67 66 66 67 67 68 66 67 66 66 66 67 65 65 66 66 66 64 65 65 66 66 67 67 68 68 66 66 65 66 65 66 67];
row6=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col6= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row7=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col7= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row8=[2:2:126]
col8= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
coord=[2:2:126];
times=[1:63];
AOI_unique=unique(AOI);
num_AOI=length(AOI_unique);
colors={'r' 'g' 'b' 'c' };
%while num_AOI>length(colors)
hplt=plot(times,coord, 'DisplayName', 'coord\_pos\_sibs\_xfpi');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
datetick('x','HH:MM:SS', 'keeplimits')
title(['Position of SIBS on FPI'])
xlim([min(times), max(times)])
xlabel('Time (UT)');
ylabel('Value');
hold on
for i=1:length(AOI_unique)
if AOI_unique(i)==1
%%Plot
hplt=plot(times, col1, colors{i}, 'DisplayName', 'column 1');
hold on
hplt=plot(times, row1, 'c--', 'DisplayName', 'row 1');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==2
%%Plot
hplt=plot(times, col2, colors{i}, 'DisplayName', 'column 2');
hold on
hplt=plot(times, row2, 'g--', 'DisplayName', 'row 2');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==3
%%Plot
hplt=plot(times, col3, colors{i}, 'DisplayName', 'column 3');
hold on
hplt=plot(times, row3, 'b--', 'DisplayName', 'row 3');
hold on
hleg=legend('-DynamicLegend');
grid on
elseif AOI_unique(i)==4
%%Plot
hplt=plot(times, col4, colors{i}, 'DisplayName', 'column 4');
hold on
hplt=plot(times, row4, 'r--', 'DisplayName', 'row 4');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==5
%%Plot
hpltA=plot(times, col5, colors{i}, 'DisplayName', 'column 5');
hold on
hplt=plot(times, row5, 'c--', 'DisplayName', 'row 5');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==6
%%Plot
hplt=plot(times, col6, colors{i}, 'DisplayName', 'column 6');
hold on
hpltA=plot(times, row6, 'g--', 'DisplayName', 'row 6');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==7
%%Plot
hplt=plot(times, col7, colors{i}, 'DisplayName', 'column 7');
hold on
hplt=plot(times, row7, 'b--', 'DisplayName', 'row 7');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==8
%%Plot
hplt=plot(times, col8, colors{i}, 'DisplayName', 'column 8');
hold on
hplt=plot(times, row8, 'r--', 'DisplayName', 'row 8');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
end
i=i+1;
end

Accepted Answer

Richard McCulloch
Richard McCulloch on 27 Jul 2013
So I think this might be what you're looking for. I commented out the 'i' redefinition at the very bottom and added 'figure' but to answer the coloring question I recommend using the mod (for modulus) command. If you specify
color{mod(i-1,4)+1}
the values, for i=1:8 for example, will go:
color{1}
color{2}
color{3}
color{4}
color{1}
color{2}
color{3}
color{4}
and the pattern will keep repeating for varying 'i' definitions. Here is the modified code:
AOI=[0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 8 8 8 8 8 8];
row1=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col1= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row2=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col2= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row3=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col3= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row4=[25 25 25 25 26 26.5 27 25 27 25 26 27 26 25 27 24 24 25 26 26 26 27 27 27 28 28 24 24 25 25 25 25 25 25 25 25 25 26 26.5 27 25 27 26 25 27 24 24 25 26 26 26 27 27 27 28 28 24 24 25 25 25 25 25];
col4= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row5=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col5= [66 67 67 68 66 67 66 66 66 67 65 65 66 66 66 64 65 65 66 66 67 67 68 68 66 66 65 66 65 66 67 66 66 67 67 68 66 67 66 66 66 67 65 65 66 66 66 64 65 65 66 66 67 67 68 68 66 66 65 66 65 66 67];
row6=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col6= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row7=[35 35 35 35 36 36.5 37 35 37 35 36 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35 35 35 35 35 36 36.5 37 35 37 36 35 37 34 34 35 36 36 36 37 37 37 38 38 34 34 35 35 35 35 35];
col7= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
row8=[2:2:126];
col8= [56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47 56 56 57 57 58 56 57 56 56 56 57 45 45 46 46 46 44 45 45 46 46 47 47 48 48 46 46 45 46 45 46 47];
coord=[2:2:126];
times=[1:63];
AOI_unique=unique(AOI);
num_AOI=length(AOI_unique);
colors={'r' 'g' 'b' 'c' };
%while num_AOI>length(colors)
figure
hplt=plot(times,coord, 'DisplayName', 'coord\_pos\_sibs\_xfpi');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
datetick('x','HH:MM:SS', 'keeplimits')
title('Position of SIBS on FPI')
xlim([min(times), max(times)])
xlabel('Time (UT)');
ylabel('Value');
hold on
for i=1:length(AOI_unique)
if AOI_unique(i)==1
%%Plot
hplt=plot(times, col1, colors{mod(i-1,4)+1}, 'DisplayName', 'column 1');
hold on
hplt=plot(times, row1, 'c--', 'DisplayName', 'row 1');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==2
%%Plot
hplt=plot(times, col2, colors{mod(i-1,4)+1}, 'DisplayName', 'column 2');
hold on
hplt=plot(times, row2, 'g--', 'DisplayName', 'row 2');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==3
%%Plot
hplt=plot(times, col3, colors{mod(i-1,4)+1}, 'DisplayName', 'column 3');
hold on
hplt=plot(times, row3, 'b--', 'DisplayName', 'row 3');
hold on
hleg=legend('-DynamicLegend');
grid on
elseif AOI_unique(i)==4
%%Plot
hplt=plot(times, col4, colors{mod(i-1,4)+1}, 'DisplayName', 'column 4');
hold on
hplt=plot(times, row4, 'r--', 'DisplayName', 'row 4');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==5
%%Plot
hpltA=plot(times, col5, colors{mod(i-1,4)+1}, 'DisplayName', 'column 5');
hold on
hplt=plot(times, row5, 'c--', 'DisplayName', 'row 5');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==6
%%Plot
hplt=plot(times, col6, colors{mod(i-1,4)+1}, 'DisplayName', 'column 6');
hold on
hpltA=plot(times, row6, 'g--', 'DisplayName', 'row 6');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==7
%%Plot
hplt=plot(times, col7, colors{mod(i-1,4)+1}, 'DisplayName', 'column 7');
hold on
hplt=plot(times, row7, 'b--', 'DisplayName', 'row 7');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
elseif AOI_unique(i)==8
%%Plot
hplt=plot(times, col8, colors{mod(i-1,4)+1}, 'DisplayName', 'column 8');
hold on
hplt=plot(times, row8, 'r--', 'DisplayName', 'row 8');
hold on
hleg=legend('-DynamicLegend');
set(hleg, 'FontSize',10);
grid on
end
% i=i+1;
end

More Answers (0)

Categories

Find more on Line Plots 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!