While running simulation, plot each curve with a different color
6 次查看(过去 30 天)
显示 更早的评论
Hi all, I saw many thread on the same question but I guess I don't understand how it works. I want to get different possibilities for the phi matrix so I'm running simul and want a plot with all the curves with a different color. I don't know if the problem come from where I put the plot and hold on but would really appreciate some help ! This is a MWE.
{
nb=[];
total=[];
con=[];
nbcon=[];
simul=[];
hold all
for simul=1:5
phi=zeros(4,4);
for nbIter=1:30
Nel = numel(phi);
Rindices = randperm(Nel);
N10 = floor(Nel/10);
phi(Rindices(1:N10)) = 1
nbcon=sum(sum(phi))
nb=sum(sum(phi))*rand(1);
con=[con; nb, nbcon];
end
plot(con(:,2),con(:,1),'color', rand(1,3))
end
hold off
0 个评论
采纳的回答
Geoff Hayes
2014-7-22
The problem is that the code is plotting all the data at each iteration, including that which was plotted at previous iterations. So all points are being overwritten with the new random colour.
Just reset con to the empty matrix on each iteration of the outer for loop, that way the code doesn't hang on to the legacy data from previous iterations
for simul=1:5
phi=zeros(4,4);
con = []; % new line of code to add
% etc.
Try the above and see what happens!
3 个评论
Geoff Hayes
2014-7-22
Just create a figure and add hold on after it to retain the current graph when adding new graphs. Replace the
hold all
with
figure;
hold on;
Joseph Cheng
2014-7-22
Perhaps use a colormap to get the color to get some distinct colors to plot.
Linecolor = hsv(numberoflines);
in the held plot
plot(con(iteration,2),con(iteration,1),'color', Linecolor(iteration,:))
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!