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

90 ビュー (過去 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 Center および File Exchange2-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