How can I add an additional overall legend for additional data overlaid on a 3x1 subplot, which already has a legend for each of the 3 plots?

3 views (last 30 days)
I'm working on a script that creates a 3x1 subplot of a variety of data read in from text files. The final plot dynamically changes, depending on what data is read in from the file. Once each of them has been plotted, I need to overlay each of the plots with symbols denoting different actions taken over the course of the plot. These actions will need a legend associated with them. However, only 1 legend is necessary because they are all the same symbols. Each of the subplots has its own legend as well, corresponding with data from each of the subplots. I'm wondering if it's possible to have a fourth legend on this plot for the symbols that are overlaid on the 3 plots. When these symbols are overlaid on the plots, there are 8 possible symbols. Not all have to be used and it is not known before which will be used. I was thinking I could have a hard coded legend for all 8 possible symbols placed on the side of the subplot, and then all are noted even if they're not all used. Any ideas on how to go about this would be appreciated. Thank you. I don't know that it's helpful to add the code I'm working on here, as its very long, but I would be happy to share it if it would help.
  4 Comments
dpb
dpb on 5 Aug 2013
Again, make a toy example w/ random data that runs standalone that gives the gist of what you'd like. Shouldn't take but a fraction of the above; you don't need all the fancies; just the basic idea.
Help the helper...
Sarah
Sarah on 5 Aug 2013
Ok, I hope this helps. Thank you again for looking at this. This will just be 1 plot, with data overlaid on the plot. I imagine that if I can have a "super legend" along with a legend for 1 plot, it should translate to the 3x1 stripchart. The other thing I was thinking of, and since I'm still relatively new to matlab, maybe someone could give an opinion, is on whether I should overlay the second plot on the first using the axes command? Then I could have a separate legend for that second axis and the first. Let me know if this would be a good ideas as well. Thanks.
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{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
end
% Select which commands you would like to overlay on the centroid plot by
% choosing 'true', otherwise put 'false'
commands.nodA.toggle = true;
commands.nodB.toggle = true;
commands.nodX.toggle = true;
commands.dither.toggle = true;
commands.LOS.toggle = true;
commands.ta_pos_goto.toggle = true;
commands.coord_init.toggle = true;
commands.coord_correct.toggle = true;
x = 0:62;
N = numel(x);
nodA = rand(1,N);
nodB = 5.*rand(1,N)+5;
nodX = 50.*rand(1,N)-50;
dither = rand(1,N);
LOS = 5.*rand(1,N)+5;
ta_pos_goto = 50.*rand(1,N)-50;
coord_init = 5.*rand(1,N)+5;
coord_correct = 50.*rand(1,N)-50;
% This checks if the user-selected toggle is true and then plots the
% appropriate command times on the figure
if commands.nodA.toggle == true;
hplt = plot(nodA,times);
end
if commands.nodB.toggle == true;
hplt = plot(nodB,times);
end
if commands.nodX.toggle == true;
hplt = plot(nodX,times);
end
if commands.dither.toggle == true;
hplt = plot(dither,times);
end
if commands.LOS.toggle == true;
hplt = plot(LOS,times);
end
if commands.ta_pos_goto.toggle == true;
hplt = plot(ta_pos_goto,times);
end
if commands.coord_init.toggle == true;
hplt = plot(coord_init,times);
end
if commands.coord_correct.toggle == true;
hplt = plot(coord_correct,times);
end

Sign in to comment.

Accepted Answer

dpb
dpb on 5 Aug 2013
Edited: dpb on 6 Aug 2013
I had an example much more like the following in mind...
x=1:10; y=rand(12,10); % make up some data to plot
l=cellstr(num2str([1:size(y,1)]')); % and labels for them...
ho=subplot(3,1,1:3); po=get(ho,'position') % To get the positions overall
po =
0.1300 0.1100 0.7750 0.8150
delete(ho) % don't want, just positions
for i=1:3 % plot some of y on each
i1=3*i-2;i2=3*i;
hs(i)=subplot(3,1,i);
plot(x,y(i1:i2,:))
legend(l(i1:i2)) % label the ones...
end
for i=1:3 % ok, now shrink the plots to make
p=get(hs(i),'position'); % room for the added axes to hold
p(3)=0.675; % the super legend
set(hs(i),'position',p)
end
hl=axes('position',[0.85 0.11 .105 0.815]); % and that axes
hlin=plot(hl,x,y); set(hlin,'visible','off'); % can't legend w/o lines
set(hl,'visible','off') % don't want to see axis
legend(hl,l) % and the global legends
That something like the effect you were looking for? (See how little actual was req'd to display something to look at, btw? I did get it into 20 lines! :) Try to pare your questions down to the essence instead of a whole application for somebody to try to wade through).
Obviously, I did some "magic numbers" of the sizes for the position; that's why I got the overall positions vector to begin with--I could look at those numbers and figure out a set of values to use to get the effect. You can use more sophisticated computational ways to generate them based on actual size and code there.
As always, "salt to suit..." :)
NB: I did NOT take the time to match the line styles; I just let them cycle automatically. If you control which style matches which line in the overall with those in each subplot then the legends will match, too.

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!