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.
, .
I need to concatenate all such plots into one plot as shown below:
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!
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 thehggroup
instead of being children of theaxes
directly) thus allowing you to apply certain settings to the entire group simultaneously.Here's a simple example of how this works:
Results in:
Whereas:
Where
LegendGroupLatest
is:Results in:
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!), buthggroup
prevents that.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:
This is just for padding with NaNs...
Concatenating:
plotting