How to make a colour gradient with 60 lines on a graph
90 views (last 30 days)
Show older comments
I have a program that puts 60 lines onto a singular graph. The 60 lines are the same equation being plotted with one variable changing for each iteration using a simple for loop. I was wondering if there was a way to make the lines change colours in a gradient so that the graph is easier to read by implementing a key. For example, as the value increases the colour would change gradually from yellow to orange to, finally, red. Here is the program for reference:
format long
%DECLARE VARIABLES
M = 10000002;
w = zeros(M,1);
e = zeros(M,1);
dw = zeros(M-1,1);
de = zeros(M-1,1);
T = [0:10000000];
e(1) = 0.0549;
w(1) = 0.3844e9;
for g = 12.0:0.1:18.0
for m = 0:10000000
de(m+1) = -(4e41 + (2.e40)/g) * ((w(m+1))^(-13/2)) * e(m+1);
dw(m+1) = -((8.1e41)*(e(m+1)^2) + ((1.2e40)/g)) * (w(m+1))^(-11/2);
e(m+2) = e(m+1) + (de(m+1) * 3.154e7);
w(m+2) = w(m+1) + (dw(m+1) * 3.154e7);
end
e = e(1:end-1);
w = w(1:end-1);
plot (T, w)
hold on
end
hold off
0 Comments
Accepted Answer
Voss
on 20 May 2022
You can set the ColorOrder property of the axes to control the colors of the plotted lines, without having to specify each line's color when it's plotted.
Something like this (here I've reduced the number of for loop iterations by a few orders of magnitude):
format long
%DECLARE VARIABLES
M = 1002;%0002;
w = zeros(M,1);
e = zeros(M,1);
dw = zeros(M-1,1);
de = zeros(M-1,1);
T = [0:1000];%0000];
e(1) = 0.0549;
w(1) = 0.3844e9;
% set the axes ColorOrder:
cmap = flip(autumn(61),1); % yellow -> red, with 61 colors (for 61 lines)
set(gca(),'ColorOrder',cmap)
hold on
for g = 12.0:0.1:18.0
for m = 0:1000%0000
de(m+1) = -(4e41 + (2.e40)/g) * ((w(m+1))^(-13/2)) * e(m+1);
dw(m+1) = -((8.1e41)*(e(m+1)^2) + ((1.2e40)/g)) * (w(m+1))^(-11/2);
e(m+2) = e(m+1) + (de(m+1) * 3.154e7);
w(m+2) = w(m+1) + (dw(m+1) * 3.154e7);
end
e = e(1:end-1);
w = w(1:end-1);
plot (T, w)
% hold on
end
hold off
4 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!