1
votes

I have several 2D-plots in MATLAB. In each plot there are some lines (each line is a row-vector of values of fixed length). There is always a base line (black one) and the remaining colored lines may or may not be present.

plot 1 , plot 2.

I need to concatenate all such plots into one plot as shown below: concatenated plot

Please note these are just for representational purpose but explain my problem well. I am not able to figure how to do it. Anybody got an idea? An example may be? Also, there has to be a vertical gap between the successively concatenated plots as is shown in last figure. Some points to note:

  • y-axis is of fixed length for all plots
  • if x-axis of each individual plot is 1:m. Then x-axis of final concatenated plot is 1:(n*m), where n is the number of individual plots to be concatenated.

Also, since each colored line corresponds to a specific kind of data, how to create its legend? Thanks!

2
If you want your final point on the x-axis to be n*m, then you can't have a vertical gap as this would introduce more points on the x axis. However, you can do this but you would have to eliminate some of the points for each of your signals to make room for the vertical gap, and you can certainly make the last point on the x axis n*m. If eliminating points on each signal isn't an option, then you will the final x point may have to be something like n*m + 20 where we have 20 points to allow for all of the gaps in between the plots. Is this something that could work? - rayryeng
@rayryeng i think if i choose not to plot line between the endpoint of first line and starting point of next this can be avoided. The x-axis would still be n*m. Isn't it? lets say: plot(x(1:10)); plot(x(11:20)) and so on may be... - learner
Doing x(1:10) then x(11:20) is the same as x(1:20). If you want to have a vertical gap and still keep the amount of points to be 20, you have to remove some of the points. For example, if you want to have a 2 element gap between the two signals, you could do x(1:9), then x(12:20) - rayryeng
just a small question, what labels do you want to have on the x-axis? - bla
@natan - That's a perfectly valid question and I didn't think to ask that question myself! - rayryeng

2 Answers

2
votes

I see two options here: 1. concatenate to the same plot and pad with NaNs to obtain the gap. 2. actually have several plots and use axes in a clever way.

Here's an example for option 1, First we'll create some fake data:

a1=rand(1,20);
b1=3+rand(1,20);
c1=6+rand(1,20);

a2=rand(1,20);
b2=3+rand(1,20);
c2=6+rand(1,20);

a3=rand(1,20);
b3=3+rand(1,20);
c3=6+rand(1,20);

This is just for padding with NaNs...

f=@(x) [ NaN(1,round(numel(x)/5)) x ];

Concatenating:

y1=[f(a1) f(a2) f(a3)];
y2=[f(b1) f(b2) f(b3)];
y3=[f(c1) f(c2) f(c3)];

plotting

x=1:numel(y1);
plot(x,y1,x,y2,x,y3);
set(gca,'XTickLabel',[]); 

enter image description here

2
votes

This is regarding the legend part of your question:

In order to have a single legend entry for several, separately-plotted items (the more accurate term would be "children of the axes object"), you should use hggroup. This way, plotted objects (such as lines) are grouped together (technically they become children of the hggroup instead of being children of the axes directly) thus allowing you to apply certain settings to the entire group simultaneously.

Here's a simple example of how this works:

%// Without hggroup
figure(1337); hold all;
x = linspace(-pi/2,pi/2,200);
for ind=1:3
    plot(x,sin(ind*x+ind),'DisplayName',...
         ['sin(' num2str(ind) 'x+' num2str(ind) ')']);
end
legend('-DynamicLegend','Location','NorthWest');

Results in:

Plot with a dynamic legend without hggroup

Whereas:

%// With hggroup
figure(1338); hold all;
x = linspace(-pi/2,pi/2,200);
prePlot=length(get(gca,'Children'));
for ind=1:3
    plot(x,sin(ind*x+ind),'DisplayName',...
         ['sin(' num2str(ind) 'x+' num2str(ind) ')']);
end
postPlot=length(get(gca,'Children'));
meshGrp = LegendGroupLatest(gca,postPlot-prePlot);
set(meshGrp,'DisplayName','Some sines');
legend('-DynamicLegend','Location','NorthWest');

Where LegendGroupLatest is:

function grpName=LegendGroupLatest(ax_handle,howMany)    
    grpName=hggroup; 
    tmp=get(ax_handle,'Children'); set(tmp(2:howMany+1),'Parent',grpName);
    set(get(get(grpName,'Annotation'),...
                'LegendInformation'),'IconDisplayStyle','on');

Results in:

Plot with a dynamic legend including hggroup

In this example, all lines that were plotted inside the loop get added to a single hggroup without affecting previously drawn items, and you can obviously add different logic to assign plots to groups.

Notice that a dynamic legend usually adds any line that is present in the chart (if you draw a zoom box in an axes that has a dynamic legend - the zoom box borders temporarily get added to the legend!), but hggroup prevents that.