How can I change the line color (e.g. 'color','g' doesn't work)

44 views (last 30 days)
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

Accepted Answer

dpb
dpb on 19 Jul 2016
Edited: dpb on 19 Jul 2016
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:
  2 Comments
Stephen23
Stephen23 on 20 Jul 2016
Kevin Wolf's "Answer" moved here:
Thanks a lot for your support. I tried diffierent ways to plot the data but it's always the same: I can change the colour of my marker points but not the line itself. Here is the new code, where I tried to use just one loop:
load('Ergebnisse');
b_A=1;
b_A_Spalte=2;
n=100;
n_Spalte=1;
b_B_Spalte=3;
T_B_Spalte=4;
YSpalte=7;
XSpalte=4;
b_A_Werte=unique(Ergebnisse(:,b_A_Spalte));
b_A_Anzahl=length(b_A_Werte);
T_B_Werte=unique(Ergebnisse(:,T_B_Spalte));
T_B_Anzahl=length(T_B_Werte);
Ergebnisse(Ergebnisse(:,n_Spalte)~=n,:)=[]; %nur bestimmte Stichprobengröße n auswerten
for b_A=1:b_A_Anzahl
b_A_aktuell=b_A_Werte(b_A);
Erg=Ergebnisse;
Erg(Erg(:,b_A_Spalte)~=b_A_aktuell,:)=[]; %nur gewähltes b_A
b=Ergebnisse(:,b_B_Spalte);
b_B_Werte=unique(Ergebnisse(:,b_B_Spalte));
b_B_Werte(b_B_Werte(:)==b_A_aktuell)=[]; %b_B muss ungleich b_A sein
b_B_Anzahl=length(b_B_Werte);
Erg2=sortrows(Erg,XSpalte);
Erg3=sortrows(Erg2,b_B_Spalte);
y1=Erg3(1:8,YSpalte);
y2=Erg3(9:16,YSpalte);
y3=Erg3(17:24,YSpalte);
y4=Erg3(25:32,YSpalte);
s=4;
x=unique(Erg3(:,XSpalte));
subplot(3,2,b_A_aktuell);
hold on
h=plot(x,y1,'mx-',x,y2,'ro--',x,y3,'bs-.',x,y4,'kd:');
grid on
set(gca,'XTick',[0.5 1 2 3 4 5 10])
xlabel('T_B')
ylabel('Anteil an Zensierungen[%]')
ylim([0,100])
xlim([0.25,10])
legend('b_B=1','b_B=2','b_B=3','b_B=4')
title(['b_A','=',num2str(b_A_aktuell),' ','n','=',num2str(n)],'position',[1 101 0],'FontSize',10)
end
I attached the sheet with the data. Thanks a lot for your help!!!
dpb
dpb on 20 Jul 2016
Edited: dpb on 20 Jul 2016
Unfortunately, my browser and the Answers site don't see eye-to-eye on the links to non-text files; I can't download the spreadsheet file correctly.
It looks like the same issue exists in the revised code, though, you're overwriting line handles and using gca plus *legend*inside a loop. I'm guessing that the order is causing some issues.
What happens if you run the example I gave exactly on a new figure?
ADDENDUM
Oh, I see I neglected to paste the demo code; I used your arrays for c, l, m and the value n as you defined them, then just created a plot for testing purposes.
x=1:10; y=randn(10,4); % arbitrary data
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')
Let's go back to basics and see if it's a system-dependent issue on the graphics. If the above on a clean figure (and perhaps a new Matlab session "just in case") doesn't reproduce the correct results, then it's TMW support time.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!