How can I change the color of the plot in each iteration in MATLAB?

后端 未结 2 688
长发绾君心
长发绾君心 2021-01-18 20:12

The following is a part of my matlab code. As it\'s shown, I would like to plot 8 curves in one plot. But I want to make each curve with one unique color. I also want to cha

相关标签:
2条回答
  • 2021-01-18 20:41

    Just use hold all instead of hold on and put the legend labels in a cell array

    hold all
    for i=1:8
        .
        .
        .
        plot(b,r);
    
        Leg{i} = ['qho-',num2str(i)];    
    
    end
    legend(Leg)
    

    See this question for example: Sparse matrix plot matlab


    NOTE:

    From Matlab R2014b onward, hold on has been modified to act like hold all, i.e. change the colours of the plots each time one is plotted. The docs state that the hold all syntax will be removed in future releases.

    0 讨论(0)
  • 2021-01-18 20:48

    How about something like:

    figure, hold on
    N = 8;
    h = zeros(N,1);    %# store handle to line graphic objects
    clr = lines(N);    %# some colormap
    for i=1:N
        %# plot random data
        y = cumsum(randn(100,1));
        h(i) = plot(y, 'Color',clr(i,:));
    end
    hold off
    legend(h, num2str((1:N)','gho-%d'))    %# display legend
    

    plot

    0 讨论(0)
提交回复
热议问题