MATLAB Answers

How can I give different colours not randomly to large number of lines in one plot?

1 ビュー (過去 30 日間)
Wiqas Ahmad
Wiqas Ahmad 2021 年 2 月 10 日
編集済み: Iuliu Ardelean 2021 年 2 月 10 日
The syntax in the code col=['r' 'y' 'b' 'g' 'k' 'c' 'm' 'k' '#D95319']; is related to the color of the lines in the plot which works well for 9 lines in the plot. But actually I want to plot 20 lines. How can I write this syntax for large number of colors? In fact, I don't want to give them random colors but in order.
clc
clear all
load EC.mat
for n=4:1:9;
filename = sprintf('data_%d.mat',n);
load(filename)
E(n-3,:)=EC(1,:);
d(n-3,:)=dep(9,:);
end
hold on ; % MOVE THIS OUTSIDE THE LOOP
col=['r' 'y' 'b' 'g' 'k' 'c' 'm' 'k' '#D95319']; % MOVE THIS OUTSIDE THE LOOP
[m,~]=size(d);
lineHandles = gobjects(1,m); % ADD THIS
for i=1:m
PP(i,:)=plot(d(i,:),E(i,:),'ok','LineWidth',2)% original data
P1(i,:) = polyfit(d(i,:),E(i,:),2);%%2 indicates the quadratic
% x_values = min(in_(1,:)): 0.001:max(x_axis(1,:));
Y= polyval(P1(i,:),d(i,:));
h1 = plot(d(i,:),Y,'*g','LineWidth',1) % fitted
Y1 = polyval(P1(i,:),d(i,:));
R1 = corrcoef(Y1,d(i,:));
%%lsline
ss = min(d(i,:)) : 0.001: max(d(i,:));
Y2 = polyval(P1(i,:),ss);
lineHandles(i) = plot(ss,Y2,'color',col(i),'LineWidth',1); % STORE HANDLE
end
% MOVE THIS OUTSIDE THE LOOP
title('Polynomial fitting','FontSize',16,'FontWeight','normal')
xlabel('Depolarisation ratio \delta_{in}','FontSize',12,'FontWeight','normal');
ylabel('Extinction coefficient \alpha{(m^-^1)}','FontSize',12,'FontWeight','normal');
ylim([0.005 0.03])
% SUPPLY HANDLES, DEFINE EACH POLY CURVE LINE
legend([PP(end,:), h1, lineHandles],...
["Actual data","Data fit",compose('PolyCurve %d\\mum',(4:m+7))],'location','Northwest')
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 10 日
col=['r' 'y' 'b' 'g' 'k' 'c' 'm' 'k' '#D95319']; % MOVE THIS OUTSIDE THE LOOP
needs to be either
col={'r' 'y' 'b' 'g' 'k' 'c' 'm' 'k' '#D95319'}; % MOVE THIS OUTSIDE THE LOOP
or
col=["r" "y" "b" "g" "k" "c" "m" "k" "#D95319"]; % MOVE THIS OUTSIDE THE LOOP
If you use the {} form then you need to use col{i} instead of col(i)

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

回答 (1 件)

Iuliu Ardelean
Iuliu Ardelean 2021 年 2 月 10 日
編集済み: Iuliu Ardelean 2021 年 2 月 10 日
Let's say you have 7 colours:
x = rand(7, 100);
y = rand(7, 100);
col = parula(7); % replace col=['r', 'y', 'b'] with col=parula(ColoursNumber)
figure
hold on
for i = 1:7
plot(x(i,:),y(i,:), 'Color', col(i,:));
end
hold off
legend
If you don't like parula, you can try different ones from this list:
Image should look like this:

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by