How to change the colors of the plotted lines in for loop?
2 次查看(过去 30 天)
显示 更早的评论
close all;
clear all;
clc;
%% ------------------------------Program-------------------------------------
z=2860:11:3135;
FOV=[1 2];
col=['r' 'b'];
[m,~]=size(FOV);
lineHandles = gobjects(1,m);
for i=1:length(FOV)
figure(1),
hold on
I0= getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_I0.dat']);
Q0= getsignal(['FOV_',num2str(FOV(i)),'mrad\out_resultsG_Q0.dat']);
I= sum(I0,2);
Q= sum(Q0,2);
dep(i,:)= (I-Q)./(I+Q);
lineHandles(i)= plot(dep,z,'color',col(i,:),'LineWidth',1.5);
end
%plot(dep,z,'LineWidth',1.5);
legend('FOV_{in}','FOV_{out}','location','Southeast')
xlabel('Depolarization ratio \delta');
ylabel('\fontname{Arial}Cloud depth (m)');
set(gca,'color','w','Fontsize',12,'LineWidth',1,'Fontweight','normal');
set(gca,'box','off','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[0 1.4],'xtick',[0:0.2:1.4],'ylim',[2850 3150],'ytick',[2850:60:3150]);
set(gca,'xgrid','on','ygrid','on','gridcolor','k')
figure (2),
hold on
depr=(dep(1,:)./dep(2,:));
plot(depr,z,'b','LineWidth',1.5);
xlabel('Depolarization ratio \delta_{rat}');
ylabel('\fontname{Arial}Cloud depth (m)');
set(gca,'color','w','Fontsize',12,'LineWidth',1,'Fontweight','normal');
set(gca,'box','off','Fontname','Arial','Fontsmoothing','on');
set(gca,'xlim',[0 1.6],'xtick',[0:0.2:1.6],'ylim',[2850 3150],'ytick',[2850:60:3150]);
set(gca,'xgrid','on','ygrid','on','gridcolor','k')
This is my program which gives the following output figure:
I am trying to change the color of these curves using the lineHandles as:
lineHandles(i)= plot(dep,z,'color',col(i,:),'LineWidth',1.5);
However it displays the following error:
2 个评论
回答(1 个)
DGM
2021-5-11
编辑:DGM
2021-5-11
You're flirting with disaster by doing this:
col=['r' 'b']; % this is literally 'rb'
As KSSV mentions, your first indexing operation selects the entire char vector 'rb', which isn't a valid color. Even if you selected only one element at a time, there are weaknesses. If you decided to change your linespec value to something other than a single character
col=['r:' 'b'];
You wouldn't get what you expect, because ':' isn't a valid color.
Instead of concatenating your linespec values into one char array, use a cell array (could also use an array of strings if your version supports it)
col = {'r','b'};
This would allow you to specify the full linespec (both linetype and color) using col.
lineHandles(i)= plot(dep,z,col{i},'LineWidth',1.5);
It might not be something you need for this one plot, but it's a safer and (at least I think so) more convenient way to do it.
On the other hand, if you wanted to specify the color independent of the linetype and were using an RGB color table, sure.
col = [0.004359 0.2834 0.2834;
0.3257 0.2739 0.8057;
0.959 0.2104 0.7873;
0.92 0.5704 0.3518;
0.7345 0.881 0.5547];
% ...
lineHandles(i)= plot(dep,z,'color',col(i,:),'linestyle',':','LineWidth',1.5);
2 个评论
DGM
2021-5-11
First things first, just remove the option from the plot routine and make sure it works. I had to modify it in order for it to work properly:
lineHandles(i)= plot(dep(i,:),z,'LineWidth',1.5);
Then, once the thing plots, debug further based on the approach you're using for color assignment.
I refer to three different approaches:
1:Single-element indexing in a character vector:
col=['r' 'b'];
% ...
lineHandles(i)= plot(dep(i,:),z,'color',col(i),'linestyle',':','LineWidth',1.5);
In this case, see what the value is for
col(i)
2: Using a cell array of chars
col = {'r','b'};
% ...
lineHandles(i)= plot(dep(i,:),z,col{i},'LineWidth',1.5);
In this case, see what the value is for
col{i}
3: indexing through rows of a color table
col = [0.004359 0.2834 0.2834;
0.3257 0.2739 0.8057];
% ...
lineHandles(i)= plot(dep(i,:),z,'color',col(i,:),'LineWidth',1.5);
In this case, see what the value is for
col(i,:)
For assurances, this is the test code:
z=2860:11:3135;
FOV=[1 2];
col = {'r','b:'};
[m,~]=size(FOV);
lineHandles = gobjects(1,m);
for i=1:length(FOV)
figure(1),
hold on
I0= rand(numel(z));
Q0= rand(numel(z));
I= sum(I0,2);
Q= sum(Q0,2);
dep(i,:)= (I-Q)./(I+Q);
lineHandles(i)= plot(dep(i,:),z,col{i},'LineWidth',1.5);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!