【问题标题】:Multiple plots in same figure in for loopfor循环中同一图中的多个图
【发布时间】:2014-07-21 03:16:42
【问题描述】:

我很抱歉,因为这似乎是一个典型的常见问题,但是,即使在处理“坚持”时,我也无法弄清楚我在这里做什么。我的情节正在被对方覆盖。我想在同一个图中为最里面的循环绘制多个图。请建议。

userName={'A' 'B' 'C'  'Z' 'R'};
timeCategories={'All' 'Morning' 'Afternoon' 'Evening'};
for user=1:5
    currentUserName= char(userName(user));    
    for K=1:4
        method='Al';
        fig=figure();
        xlabel('Recall', 'FontSize',12,'FontWeight','bold');
        ylabel('Precision','FontSize',12, 'FontWeight','bold');
       titleOfFig=strcat('PrecisionRecall\_',currentUserName,'\_','Top',num2str(K));
        title(titleOfFig,'FontSize',17,'FontWeight','bold'); 
        set(gca,'XColor',[.0 .3 .2],'YColor',[.0 .3 .2],'LineWidth',2, 'FontSize',11, 'FontWeight','bold');
        axis([0 1 0 1]);
        grid on;
        filename = sprintf('%sPrecisionRecall_%s_Top%d.png',path,currentUserName,K); %path,'PrecisionRecall\_',userName(1),'\_','Top',num2str(K),'.png');
        hold on;
        for timeCat=1:4
            timeCategory=char(timeCategories(timeCat));
            precisionFileName=strcat(method,timeCategory,'top',num2str(K),...
            'Precision.csv');
            recallFileName=strcat(method,timeCategory,'top',num2str(K),'Recall.csv');
            Ptemp= csvread(precisionFileName);
            Rtemp= csvread(recallFileName);
            precisionVector=Ptemp(user,:);
            recallVector=Rtemp(user,:);
            h= plot(recallVector,precisionVector,'r--*','MarkerSize',8);
            hold on;
            set(h,'LineWidth',2);
        end
        legend(timeCategories,'Location','Best');
        print(fig, '-dpng',filename,'-r200');
     end 
end

【问题讨论】:

  • 这可能不被视为答案,但您是否尝试过使用hold all
  • 您能否更具体地说明您的 情节正在被彼此覆盖 是什么意思?到底发生了什么?
  • 我猜你的情节没有被替换,你一次又一次地得到同样的情节。请查看您的代码。在最里面的循环中,将precisionVector=ptemp(1,:) 替换为precisionVector=ptemp(timeCat,:),并以同样的方式将recallVector=Rtemp(1,:) 替换为recallVector=Rtemp(timeCat,:)。然后你会得到正确的。
  • @Naveen - 不错的收获。那应该解决它
  • @Naveen 他已经定义了一个依赖于KtimeCat 的新csv。 csv中的值是否不同?

标签: matlab loops plot figure


【解决方案1】:

就像 Naveen 指出的那样,您没有使用循环索引;)请注意,您只需为每个图形设置一次 hold on。所以在你的 timeCat 循环中保持是不必要的。在您打开新图或取消暂停之前保持暂停。

添加了通用数据以显示以下代码工作正常。问题一定出在您的数据中,因为您的数据不依赖于 timeCat。

@ironzionlion 是的,确实每个for循环都分配了变量P,但在循环期间变量是相同的。因此我假设他忘记使用变量 timeCat 来识别正确的行。但是,您可能调用了错误的文件。他的其余代码工作正常,正如您在我使用通用数据的代码中看到的那样。

请检查您从 CSV 文件中读取的数据,并了解如何访问这些数据。

亲切的问候,

恩斯特·扬

close all
clear all

userName={'A' 'B' 'C'  'Z' 'R'};
timeCategories={'All' 'Morning' 'Afternoon' 'Evening'};
for user=1:5
    currentUserName= char(userName(user));    
    for K=1:4
        method='Al';
        fig=figure();
        xlabel('Recall', 'FontSize',12,'FontWeight','bold');
        ylabel('Precision','FontSize',12, 'FontWeight','bold');
        titleOfFig=strcat('PrecisionRecall\_',currentUserName,'\_','Top',num2str(K));
        title(titleOfFig,'FontSize',17,'FontWeight','bold'); 
        set(gca,'XColor',[.0 .3 .2],'YColor',[.0 .3 .2],'LineWidth',2, 'FontSize',11, 'FontWeight','bold');
%         axis([0 1 0 1]);
        grid on;
        %filename = sprintf('%sPrecisionRecall_%s_Top%d.png',path,currentUserName,K); %path,'PrecisionRecall\_',userName(1),'\_','Top',num2str(K),'.png');
        hold on;
        for timeCat=1:4
            timeCategory=char(timeCategories(timeCat));
            precisionFileName=strcat(method,timeCategory,'top',num2str(K),...
            'Precision.csv');
            recallFileName=strcat(method,timeCategory,'top',num2str(K),'Recall.csv');
            Ptemp= timeCat.*[1:10];
            Rtemp= (1:10)/10;
            %precisionVector=Ptemp(1,:); % Call with loop variable!
            precisionVector=Ptemp;
            %recallVector=Rtemp(1,:); % Call with loop variable!
            recallVector=Rtemp;
            h= plot(recallVector,precisionVector,'r--*','MarkerSize',8);
            % hold on; You don't need this one I think.
            set(h,'LineWidth',2);
        end
        legend(timeCategories,'Location','Best');
        filename = [num2str(K),num2str(user)];
        print(fig, '-dpng',filename,'-r200');
     end 
end

【讨论】:

  • 谢谢,但是,PtempRtemp 已经依赖于循环变量,因为要读取的 csv 文件依赖于循环变量。
  • 我想知道谁否决了一个可行的解决方案。无论如何,答案中提供的代码(使用通用数据)为每个图绘制了四行(总共 20 行)。因此工作正常。请逐步编辑上述代码,以便您确定问题所在。
猜你喜欢
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 2012-08-28
  • 2018-11-28
  • 2011-10-25
  • 2013-04-24
相关资源
最近更新 更多