![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/638565/image.png)
Color gradient for graph in a for loop
조회 수: 19 (최근 30일)
이전 댓글 표시
Hi everyone!
I'm plotting 3 force vs displacement plots in the same picture. For one of the lines in the plot, i'd like the color to gradually chance, so to be able to identify the change of the curve throughout the iterations. I would also like the legend of the figure to show a color bar which indicated e.g that the color becomes darker/lighter as the iteration progresses.
So far I have something like this (this is a pseudocode ofc):
fe = 1:0.1:100;
n_freq = length(fe);
for kk= 1:n_freq
[r,z] = ode23s('Nonlin', tspan x0);
x = z(:,1);
F = z(:,2);
F2 = z(:,3);
Ftot = F + F2;
figure(1)
hold on
plot(x, Ftot) %this is the plot which I would like to change gradually per each iteration of the for loop
hold on
plot(x, F)
hold on
plot(x,F2)
end
Can someone help me out? Thanks!
댓글 수: 0
채택된 답변
Mathieu NOE
2021년 6월 1일
hello
this would be my suggestion , gray dark to light for one curve
I wonder if you overlay 3 curves at each iteration if it's not ging to be overcrowed
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/638565/image.png)
fe = 1:1:100;
n_freq = length(fe);
%% main code
map = colormap('gray');
[mmap,nmap] = size(map);
data_min = 0;
data_max = n_freq;
figure(1)
for k= 1:n_freq
% [r,z] = ode23s('Nonlin', tspan x0);
% x = z(:,1);
% F = z(:,2);
% F2 = z(:,3);
%
% Ftot = F + F2;
% % dummy code demo
x = fe;
Ftot = k*fe; % dummy demo output
hold on
h = plot(x, Ftot); %this is the plot which I would like to change gradually per each iteration of the for loop
% now define col value based on data value (min data value maps to colormap map index 1
% and max data value maps to colormap map last index);
ind = fix(1+(mmap-1)*(k-data_min)/(data_max-data_min));
h.Color = map(ind,:);
end
% optionnal colorbar
caxis([0,n_freq])
CBAR_ticks = 10*(0:ceil(n_freq/10));
hcb=colorbar('Ticks',CBAR_ticks,'TickLabels',split(num2str(CBAR_ticks)));
hcb.Title.String = "Iteration #";
hcb.Title.FontSize = 13;
댓글 수: 0
추가 답변 (1개)
darova
2021년 6월 1일
Wha about this?
x = [0:.2:10 nan];
y = sin(x);
patch(x,y,y,'edgecolor','interp','linewidth',2)
colorbar
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!