1

I am attempting to plot a subplot within another subplot in MATLAB. The problem is that the final subplot shows only portions of the smaller subplot. After some searching on the web, it seems one option here is to save the subplot into a temporary figure and add it to the final subplot after. Another way is to manually specify positions of the plots inside the final figure i.e. rolling your own subplot. I was wondering if there were a more elegant approach. Apologies in advance if this isn't possible/desirable behavior. My idea of how the code would run is:

someData = linspace(0,10);
subplot(2,2,1);
plotThing(someData);
subplot(2,2,2);
plot(cos(someData));

function [ out ] = plotThing( someData )
   y1 = sin(someData);
   y2 = sin(2*someData);
   y3 = sin(4*someData);
   y4 = sin(8*someData);
   f = figure(4);

   subplot(2,2,1)
   plot(someData,y1)
   subplot(2,2,2)
   plot(someData,y2)
   subplot(2,2,3)
   plot(someData,y3)
   subplot(2,2,4)
   plot(someData,y4)

   out = f;
end

My desired behavior would be to have two things in the final figure, the subplot of four items on top, and the single plot underneath. Any advice would be great.

EDIT - The issue with the accepted answer is that it would require a significant number of subplots (12x12 in my situation). After some thought and work, I discovered that it is possible to encapsulate the contents of a subplot into a uipanel. This gives the desired behavior of a subplot being able to be treated as a unit in a larger scale figure. The code to accomplish this looks something like:

plotThing(someData, [0, 0.5, 1, 0.5]);
panel2 = uipanel('Position', [0, 0, 1, 0.5]);
subplot(1,1,1,'Parent', panel2); %Strange but necessary as one cannot set the parent of a plot directly
plot(cos(someData));

function [ out ] = plotThing( someData, position )
    panel = uipanel('Position', position); 

    y1 = sin(someData);
    y2 = sin(2*someData);
    y3 = sin(4*someData);
    y4 = sin(8*someData);

    subplot(2,2,1, 'Parent', panel)
    plot(someData,y1)
    subplot(2,2,2, 'Parent', panel)
    plot(someData,y2)
    subplot(2,2,3, 'Parent', panel)
    plot(someData,y3)
    subplot(2,2,4, 'Parent', panel)
    plot(someData,y4)
end

The result of this generalized solution look like: results

3 Answers 3

3

You should be able to do this by telling MATLAB the span of your subplots as follows.

subplot(4,2,1);  % create a plot with subplots in a grid of 4 x 2
plot(someData,y1); % subplot at first row, first column
subplot(4,2,2);
plot(someData,y2); % subplot at first row, second column
subplot(4,2,3);
plot(someData,y3);
subplot(4,2,4);
plot(someData,y4);
subplot(4,2,[5 6 7 8]); % subplot spanning the entire third and fourth row
plot(someData,y5);      % change [5, 6, 7, 8] to change the span
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks so much for taking time to answer the question. Your solution is a good one. However, in providing a generic example I neglected to indicate that my 'small' subplot uses a special subplot (one from the FEX that removes spacing between plots) and I was rather hoping to keep that formatting while still using the normal subplot for the 'big' subplot. Is it somehow possible to treat the small subplot as one unit and manipulate it from there?
Provide a link to the file exchange subplot. Also, do you mean you want to use the FEX subplot for all of your subplots (big and small) and yet achieve what you have mentioned?
mathworks.com/matlabcentral/fileexchange/39664-subtightplot. I use the tight subplot for the 'small' plot, but would like to use the stock subplot for the 'big' plot.
If you see this, it shows how to plot a big plot and small plots together. Why don't you use that. The syntax is same as above. You just have to 'overload' subplot.
I don't know why I didn't consider that. Thanks so much for your help.
0

You would still use the subplot command, just use a vector for the position if you want it to take up more than 1 space:

subplot(2,4,1)
plot(someData,y1)
subplot(2,4,2)
plot(someData,y2)
subplot(2,4,3)
plot(someData,y3)
subplot(2,4,4)
plot(someData,y4)

subplot(2,4,[5:8])
plot(bigPlot,y5)

should do it.

Comments

0

Using a fine grid and spanning bigger plots over multiple fields can be avoided: subplot can be called with different grid sizes even within the same figure. But the subplots in the smallest grid should be drawn first, otherwise I experienced some overlap (in Matlab 2017). You just have to make sure, that your grids fit together.

For example:

subplot(4,2,1)
plot(someData)
subplot(4,2,3)
plot(someData)
subplot(2,2,2)
plot(someData)
subplot(2,2,3)
plot(someData)
subplot(2,2,4)
plot(someData) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.