How to make a colour gradient with 60 lines on a graph

조회 수: 81 (최근 30일)
AJ99
AJ99 2022년 5월 20일
댓글: Voss 2022년 5월 20일
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

채택된 답변

Voss
Voss 2022년 5월 20일
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
AJ99
AJ99 2022년 5월 20일
This is incredibly useful thanks for the help again!
Voss
Voss 2022년 5월 20일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by