3

I am animating some subplots in MATLAB (R2015a). However, when I attempted to position a subplot to take up multiple positions the hold on command no longer works.

Here is a simplified form of my problem:

clear
N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);

    for n = 1:N;
        subplot(2,2,[1,2]);
        title('Particle counts at step number');
        plot(Analysis(n,1), Analysis(n,2), '.', 'Markersize', 8, 'color',      'red'), hold on;
        plot(Analysis(n,1), Analysis(n,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
        legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
        xlim([0,N]);
        ylim([0, 20]);


        subplot(2,2,[3,4]);
        title('Temperature(k) and Pressure');
        plot(Analysis(n,1), Analysis(n,4), '.', 'Markersize', 8, 'color', 'red'), hold on;
        plot(Analysis(n,1), Analysis(n,5), '.', 'Markersize', 8, 'color', 'blue');
        legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
        xlim([0,N]);
        ylim([0, 20]);
        pause(0.1);
        drawnow;
    end

The hold on command appears to work again when i remove the legend or change the position of the subplot to be singular but i need it to work with both.

  • I tried this: `subplot(2,1,1); plot(rand(1,15)) subplot(2,1,2); plot(rand(1,15)) subplot(2,1,1); hold on; plot(rand(1,15))` and it works nicely, cannot recreate your problem without having a copy of your data maybe? – GameOfThrows Dec 02 '15 at 15:38
  • Please provide a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. – sco1 Dec 02 '15 at 15:42
  • Initially I did it the way that you did and it worked fine. It stopped working when I changed the 'position' to include multiple grid positions: subplot(4,2,[5,6]); %4x2 grid, occupying positions 5 and 6. – Joseph Davies Dec 02 '15 at 15:52
  • This does seem like a bug, I'm seeing the same behavior in R2015b. I would suggest submitting a [bug report](https://www.mathworks.com/support/service_requests/contact_support.do) to The MathWorks. – sco1 Dec 02 '15 at 17:49
  • Might be helpful : http://stackoverflow.com/questions/13102654/how-should-i-update-the-data-of-a-plot-in-matlab – Andrey Rubshtein Dec 03 '15 at 00:39

1 Answers1

1

As I said above, this does appear to be a bug. One possible workaround is to modify the XData and YData properties of your line objects:

For example:

N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);

subplot(2,2,[1,2]);
title('Particle counts at step number');
hold on;
ph(1) = plot(Analysis(1,1), Analysis(1,2), '.', 'Markersize', 8, 'color',      'red');
ph(2) = plot(Analysis(1,1), Analysis(1,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
hold off;
legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0, N]);
ylim([0, 20]);

subplot(2,2,[3,4]);
title('Temperature(k) and Pressure');
hold on;
ph(3) = plot(Analysis(1,1), Analysis(1,4), '.', 'Markersize', 8, 'color', 'red');
ph(4) = plot(Analysis(1,1), Analysis(1,5), '.', 'Markersize', 8, 'color', 'blue');
hold off;
legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0 ,N]);
ylim([0, 20]);

for n = 2:N;
    k = 2;
    for ii = 1:4
        ph(ii).XData = Analysis(1:n, 1);
        ph(ii).YData = Analysis(1:n, k);

        k = k + 1;
    end

    pause(0.1);
    drawnow;
end

I believe this gives you what you're looking for. Not the prettiest but it's functional.

sco1
  • 12,154
  • 5
  • 26
  • 48
  • 1
    Also worth noting that setting XY data has generally been [much faster than repeated `plot` calls](http://stackoverflow.com/questions/13102654/how-should-i-update-the-data-of-a-plot-in-matlab). – sco1 Dec 02 '15 at 21:13
  • Thanks excaza! That works exactly how I wanted it too. I'll submit a bug report. – Joseph Davies Dec 03 '15 at 00:51