how to make the plot change color atomatic ?

8 views (last 30 days)
tomer polsky
tomer polsky on 3 Aug 2018
Commented: Luke Perry on 6 Aug 2018
for exmaple this is my code :
clc;clear all;close all;
f=[10 50 100];
T=1./f;
for i=1:length(T)
t(i,:)=linspace(0,2*T(i),10000);
subplot(3,1,i);
y(i,:)=sin(2*pi*t(i,:).*f(i));
plot(t(i,:),y(i,:),'--');
title(sprintf('y=sin(2*pi*t*%d)', f(i)));
hold on;
end
as you can see from the code the frequency changes automatically in the title ,is there a possible to change the color automatically too ? for example the first plot will be blue,the second green, and the third yellow ?

Answers (1)

Luke Perry
Luke Perry on 3 Aug 2018
Edited: Luke Perry on 3 Aug 2018
Hey Tomer,
Looking at your question I'm unsure which of the two you're looking for:
1: A kind of "heat map" where the color of the line in the plot depends on the peak values at a certain point, or magnitude of your sine curve in the plot. Obviously, you can change the parameter you're working with to match what you would want to judge the "heat" or color scheme of your plots. So, instead of magnitude, you could instead look at frequency of your sine waves, etc.
f=[10 50 100];
T=1./f;
%A list of colors from hot to cold.
colors={[1 0 0.1],[0.6 0 0.6],[0.1 0 1]};
for i=1:length(T)
t(i,:)=linspace(0,2*T(i),10000);
subplot(3,1,i);
y(i,:)=sin(2*pi*t(i,:).*f(i));
%Grab the magnitude, but if there is more than one maximum
%grab the first one. We only care about the number.
temp_magnitude=(max(y(i,:))-min(y(i,:)));
magnitudes(i)=temp_magnitude(1);
ax(i)=plot(t(i,:),y(i,:),'--');
title(sprintf('y=sin(2*pi*t*%d)', f(i)));
hold on;
end
%Normalize magnitudes.
normalizedmags=magnitudes/max(magnitudes);
%Arrange the normalized magnitudes by value (low to high)
arrangednormmags=sort(normalizedmags,'descend');
%Loop through the normalized mags and set the proper color
for i=1:length(normalizedmags)
ax(i).Color=colors{find(arrangednormmags,normalizedmags(i))};
end
2: How to loop through an assign different colors. Here's what I have for you.
clc;clear all;close all;
f=[10 50 100];
T=1./f;
%Make a list of colors. You can get creative with this
%and make a "smart" loop or array process of some kind to
%generate unique colors, or find some kind of tableau list.
colors={[0.15 0.7 0.1],[0 0 0],[0.7 0.2 0.03]};
for i=1:length(T)
t(i,:)=linspace(0,2*T(i),10000);
subplot(3,1,i);
y(i,:)=sin(2*pi*t(i,:).*f(i));
%Assign the color when plotting.
plot(t(i,:),y(i,:),'--','Color',colors{i});
title(sprintf('y=sin(2*pi*t*%d)', f(i)));
hold on;
end
  4 Comments
Stephen23
Stephen23 on 6 Aug 2018
"To make black, combine all colors [1 1 1]."
Probably you meant [0,0,0].
"I have one question how did you transfer the colors in numbers ?"
Luke Perry
Luke Perry on 6 Aug 2018
Hey thanks Stephen. It was a little too early over here to be figuring that all out in my head.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!