【问题标题】:How to update a plot with two subplot in MATLAB?如何在 MATLAB 中用两个子图更新一个图?
【发布时间】:2019-09-04 21:54:30
【问题描述】:

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

【问题讨论】:

  • 您的代码绘制了蓝线。改变线条颜色的代码在哪里?绘制两条对角线的代码在哪里?您未显示的内容可能正在删除一行。这就是为什么我们总是在这里要求minimal reproducible example。如果你努力做到这一点,我相信你会找到自己的错误......

标签: matlab plot matlab-figure subplot


【解决方案1】:

假设你的 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);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多