How can I change a single line color and style in a graph?

6 views (last 30 days)
Hi! I have created a code that will generate 3 figures, with 8 lines in each figure.
I want to change the color and style of the lines individually, but can't seem to find a way that works. Any advice would be really helpful!
The code is shown here.
phistep = 0.0001;
phiarr = (0.25:phistep:5)';
numberofvalues = (5-0.25)/phistep+1;
d11 = 65493;
d12 = 1267;
d22 = 32243;
d66 = 1867;
marr = [1,2,3,4];
narr = [1,2];
for c = [0,1,2]
linevalues = zeros(numberofvalues,length(marr)*length(narr));
for m = marr
for n = narr
for i = 1:numberofvalues
phi = phiarr(i);
idx = (m-1)*(length(narr))+n;
linevalues(i,idx) = ((pi^2)/(100^2))*(d11*(m/phi)^4 + 2*(d12+2*d66)*(m*n/phi)^2 + d22*n^4)/((m/phi)^2+c*n^2);
end
end
end
figure(c+1)
phimat = repmat(phiarr,1,length(marr)*length(narr));
[y_min, idx]=min(linevalues);
x_min = phimat(idx);
plot(phimat,linevalues,x_min,y_min,'rx');
ylim([0 600]);
xlabel('aspect ratio');
ylabel('buckling load');
grid on
legend('m=1,n=1','m=1,n=2','m=2,n=1','m=2,n=2','m=3,n=1','m=3,n=2','m=4,n=1','m=4,n=2')
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 2 Apr 2020
Try this. It uses the color specified in colors variable
phistep = 0.0001;
phiarr = (0.25:phistep:5)';
numberofvalues = (5-0.25)/phistep+1;
d11 = 65493;
d12 = 1267;
d22 = 32243;
d66 = 1867;
marr = [1,2,3,4];
narr = [1,2];
colors = [0 0 0; %% <---- Define colors
0 0 1;
0 1 0;
0 1 1;
1 0 0;
1 0 1;
1 1 0;
1 1 1]; % Give RGB value for eight lines. Each value can vary from 0 to 1
for c = [0,1,2]
linevalues = zeros(numberofvalues,length(marr)*length(narr));
for m = marr
for n = narr
for i = 1:numberofvalues
phi = phiarr(i);
idx = (m-1)*(length(narr))+n;
linevalues(i,idx) = ((pi^2)/(100^2))*(d11*(m/phi)^4 + 2*(d12+2*d66)*(m*n/phi)^2 + d22*n^4)/((m/phi)^2+c*n^2);
end
end
end
figure(c+1)
ax = axes();
hold(ax); % we need to hold the axes
phimat = repmat(phiarr,1,length(marr)*length(narr));
[y_min, idx]=min(linevalues);
x_min = phimat(idx);
% Loop is needed to specify different colors
for i=1:8
plot(phimat(:,i),linevalues(:,i),'Color', colors(i,:));
end
plot(x_min,y_min,'rx');
ylim([0 600]);
xlabel('aspect ratio');
ylabel('buckling load');
grid on
legend('m=1,n=1','m=1,n=2','m=2,n=1','m=2,n=2','m=3,n=1','m=3,n=2','m=4,n=1','m=4,n=2')
end
  2 Comments
Kurama Chan
Kurama Chan on 2 Apr 2020
Hi Ameer! Thank you for the answer it works perfect!
Is there a way that I can change the line style as well for specific lines?
Ameer Hamza
Ameer Hamza on 2 Apr 2020
After defining colors, just define a cell array of line style specifiers
lineStyles = {'-', '--', '.', ..} % make 8 elements
and then in plot statement
plot(phimat(:,i),linevalues(:,i),'Color', colors(i,:), 'LineStyle', lineStyles{i});

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!