我在MATLAB(R2015a)中制作了一些子图。但是,当我尝试定位子图以占据多个位置时,hold on
命令不再有效。
以下是我的问题的简化形式:
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
当我删除图例或将子图的位置更改为单数时,hold on命令似乎再次起作用,但我需要它同时使用它们。
答案 0 :(得分:1)
正如我上面所说,这似乎是一个错误。一种可能的解决方法是修改XData
和YData
properties of your line objects:
例如:
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
我相信这可以为您提供您所寻找的东西。不是最漂亮的,但它的功能。