MATLAB: How to change the line color (e.g. ‘color’,’g’ doesn’t work)

colorplot

Hi everyone, I have problems changing the color of the line of my plot. Even though there is the correct color of the plot in the legend, only the markers' color has the defined color, but the line itself doesn't change its color (as you can see in the attached picture). Here is an extract of my code, where I define the color of the plot:
c={'m','r','b','k'};
m={'x','o','s','d'};
l={'-','--','-.',':'};
n=4
for b_B=1:n
b_B_aktuell=b_B_Werte(b_B);
Erg2=Erg;
Erg2(Erg2(:,b_B_Spalte)~=b_B_aktuell,:)=[]; %nur aktuellen b_B-Wert
Erg3=sortrows(Erg2,XSpalte);
y=Erg3(:,YSpalte);
x=Erg3(:,XSpalte);
subplot(3,2,b_A_aktuell);
hold on
h=plot(x,y);
grid on
set(h,'color',c{b_B},'linestyle',l{b_B},'marker',m{b_B},'markerfacecolor',c{b_B},'markeredgecolor',c{b_B});
set(gca,'XTick',[0.5 1 2 3 4 5 10])
end

Best Answer

As Azzi says, it's hard to debug w/o data and would take a fair amount of work to make stuff fit your code but one thing I notice is in
h=plot(x,y);
...
set(h,'color',c{b_B}, 'linestyle',l{b_B}, ...
'marker',m{b_B}, 'markerfacecolor',c{b_B}, 'markeredgecolor',c{b_B});
...
you're overwriting the line handle h each pass through. Doesn't seem like that should be the cause just in what you've shown as it doesn't appear h is used excepting in the immediate context but I'm guessing this loop is in another loop over the number of subplots as the third argument in
subplot(3,2,b_A_aktuell);
isn't defined. So, I'm guessing your loops are overwriting the intended line handle with a previous one and consequently the line properties aren't getting set that you think are (or not for the line you think/intend, anyway).
ADDENDUM On reflection, it's not difficult to demonstrate the essence of the code with a couple modifications/corrections--
for i=1:n
h(i)=plot(x,y(:,i),'color',c{i},'linestyle',l{i}, ...
'marker',m{i},'markerfacecolor',c{i},'markeredgecolor',c{i});
if i==1,hold on,end
end
legend('A','B','C','D','location','best')
where I've just folded the line enhancements into the plot call.
NB: If, as I'm surmising, the above loop is in an outer loop, then you'll need to use a 2D array to hold the handles for each subplot or you'll be back into the same issue of overwriting them; the last array being the only one with valid handles.
OTOH, you could use the technique of set with cell arrays and do the enhancements all in "one swell foop" -- after the loop over n, then
set(h,{'color'},c', {'linestyle'}, l', ...
{'marker'},m', {'markerfacecolor'},c',{'markeredgecolor'},c');
will do it all at once. The key point here is that the cell arrays of values for each must be columnar, hence the transpose operator, (').
One last stylistic point that isn't the issue--move the loop-invariant stuff like
grid on
set(gca,'XTick',[0.5 1 2 3 4 5 10])
outside the loop; 'on' won't get any "onner" doing it over and over and there's no point in setting the tick marks to the same thing multiple times, either.
The above gives the following:
Related Question