MATLAB Answers

Color gradient for graph in a for loop

21 ビュー (過去 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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by