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

7 vues (au cours des 30 derniers jours)
Kurama Chan
Kurama Chan le 2 Avr 2020
Commenté : Ameer Hamza le 2 Avr 2020
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

Réponse acceptée

Ameer Hamza
Ameer Hamza le 2 Avr 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 commentaires
Kurama Chan
Kurama Chan le 2 Avr 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 le 2 Avr 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});

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by