필터 지우기
필터 지우기

Color gradient for graph in a for loop

조회 수: 32 (최근 30일)
GCats
GCats 2021년 6월 1일
답변: Mathieu NOE 2021년 6월 1일
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!

채택된 답변

Mathieu NOE
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
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;

추가 답변 (1개)

darova
darova 2021년 6월 1일
Wha about this?
x = [0:.2:10 nan];
y = sin(x);
patch(x,y,y,'edgecolor','interp','linewidth',2)
colorbar

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by