how to make the plot change color atomatic ?

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

that is excatly what I wanted . I have one question how did you transfer the colors in numbers ? i mean this line code :
colors={[0.15 0.7 0.1],[0 0 0],[0.7 0.2 0.03]};
Hey Tomer, this is the RGB (Red Blue Green) assignment in MATLAB. Normally the values range from 0 to 256, but MATLAB normalizes the values so they range from 0 to 1. 0 represents black (no color) while 1 represents the saturation of that base color. To make white, combine all colors [0 0 0]. Here is a link to an RGB determiner for you: https://www.rapidtables.com/web/color/RGB_Color.html (divide these by 256 for MATLAB).
Also, if you found my answer useful and that it did answer your question, please mark it as accepted. If you are still searching for something more, please explain where your question was not answered.
"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 ?"
Hey thanks Stephen. It was a little too early over here to be figuring that all out in my head.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 3 Aug 2018

Commented:

on 6 Aug 2018

Community Treasure Hunt

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

Start Hunting!