首页 > 解决方案 > 如何在 MATLAB 中用两个子图更新一个图?

问题描述

我在 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(' ')
%....

在此处输入图像描述

标签: matlabplotmatlab-figuresubplot

解决方案


假设你的roc函数计算xrocyroc我建议你重写你的代码来模块化它

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);

重写需要一点点努力,但如果您将来想要添加的不仅仅是两条曲线,它肯定会有助于使您的代码可扩展。


推荐阅读