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

7 ビュー (過去 30 日間)
Kurama Chan
Kurama Chan 2020 年 4 月 2 日
コメント済み: Ameer Hamza 2020 年 4 月 2 日
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

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 2 日
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 件のコメント
Kurama Chan
Kurama Chan 2020 年 4 月 2 日
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 2020 年 4 月 2 日
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});

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by