Control color in a for loop
12 次查看(过去 30 天)
显示 更早的评论
Hello,
this is hopefully the last question I have for now.
The function "LPplotfun" will plot the matrices LPmatx/LPmaty row for row against each other. I want each plotted line to have a different color, so that they're distinguishable. However, I don't want it to be random. Instead, the first function is supposed to be red, second blue, third green, etc. If no colors are left it is supposed to roll over to red again.
The reason is that the function "LPerrorbarfun" (if it is activated) will plot the errorbars for the data points of each plot iteration. In that case, the errorbars and respective graphs should have the same color. So I have to somehow controll which color the "LPplotfun" and "LPerrorbarfun" grab use at each iteration of the for-loop.
I tried experimenting with the "colororder"-command, but couldn't figure out how it works/how I could implement it.
Is there a way to achieve this?
Thank you.
Below is the code for both functions
%% The plotting function
function LPplotfun(LPmatx,LPmaty,LPFits,LPNumRows,LPErrorbar_Flag) %LPplotfun plots all LPfit set up by LPfitfun
hold on
for k=1:1:LPNumRows
hold on
axis([-500 500 -500 500])
plot(LPFits{1,k},'')
hold on
if LPErrorbar_Flag == 0
plot(LPmatx(k,:),LPmaty(k,:),'o')
hold on
else
hold on
end% LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
pause(0.75)
hold on
end
end
%% The errorbar function
function LPerrorbars(LPmatx,LPmaty,LPmaterrorx,LPmaterrory,LPNumRows)
%LPerrorbars plots all errorbars for the different data points
hold on
for k=1:1:LPNumRows
hold on
errorbar(LPmatx(k,:),LPmaty(k,:),LPmaterrory(k,:),LPmaterrory(k,:),LPmaterrorx(k,:),LPmaterrorx(k,:),'o')
end
hold on
end
3 个评论
Ameer Hamza
2020-3-20
Glad to be of help. I have moved my comment to answer, since it solved the issue.
采纳的回答
Ameer Hamza
2020-3-20
[Comment moved to Answer as OP comfirmed that it resolved the issue]
Why not just use the 'color' property of the line object. For example, in the function LPplotfun, before the for loop define
colors = [1 0 0; % red
0 0 1; % blue
0 1 0;]; % green
and change the plot() function call like this
plot(LPmatx(k,:),LPmaty(k,:), 'o', 'Color', colors(rem(k-1,3)+1,:))
rem() will cause the colors to loop back.
Similar solution can be used for errorbar.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!