如何在MATLAB中使用两个子图更新图?

时间:2019-04-14 21:49:23

标签: matlab plot matlab-figure subplot

我在MATLAB中有一个函数,它绘制了两条曲线,并且运行了两次。

在第一次绘制的主曲线上,您会看到红色(第一个图形),然后打开“保持”并再次用绿色(第二个形状)执行我的功能。

问题是左侧子图不起作用,删除了第一条曲线(红色曲线),但是第二条曲线却正常(最后一条曲线)。

我的主要脚本是:

% some code to processing
...
roc('r.-',data); %this function plots my curves

第二次运行

% some code to processing
...
plot on
roc('g.-',data);

我的roc函数包含:

%some code
...

  subplot(1,2,1)
    hold on
    HCO1=plot(xroc(J),yroc(J),'bo');
    hold off
    legend([HR1,HRC1,HCO1],'ROC curve','Random classifier','Cut-off 
point','Location','NorthOutside')
    subplot(1,2,2)
    hold on
    HCO2=plot(1-xroc(J),yroc(J),'bo');
    hold off
    legend([HR2,HRC2,HCO2],'ROC curve','Random classifier','Cut-off 
point','Location','NorthOutside')
    disp(' ')
%....

enter image description here

1 个答案:

答案 0 :(得分:1)

假设您的 roc 函数计算出 xroc yroc ,建议您重写代码以对其进行模块化

function [xroc,yroc] = roc(data)
%your algorithm or training code
%xroc=...
%yroc=...
end

这样,您的主脚本可以被编辑为类似的内容

%first run
[xroc1,yroc1] = roc(data);
%...some further processing with data variable
[xroc2,yroc2] = roc(data);
%plots
ax1 = subplot(1,2,1,'nextplot','add');          %create left axes
ax2 = subplot(1,2,2,'nextplot','add');          %create right axes (mirrored roc)
%now you can go ahead and make your plots
%first the not mirrored plots
plot(xroc1,yroc1,'r','parent',ax1);
plot(xroc2,yroc2,'g','parent',ax1);
%and then the mirrored plots
plot(1-xroc1,yroc1,'r','parent',ax2);
plot(1-xroc2,yroc2,'g','parent',ax2);

进行一些重写是很费力的,但是如果将来要添加两个以上的曲线,这肯定会有助于使代码可伸缩。