線形2次元plotのグラデーション

53 views (last 30 days)
RU
RU on 16 Mar 2022
Commented: RU on 17 Mar 2022
plot(x1,y1)
hold on
plot(x2,y2)
plot(x3,y3)
plot(xn,yn)
このように幾つかのグラフを同時に描くとき、1~nまでグラデーションで表すにはどうしたら良いでしょうか?

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 16 Mar 2022
>このように幾つかのグラフを同時に描くとき、1~nまでグラデーションで表すにはどうしたら良いでしょうか?
colororder関数で「色の順序」を定義すれば、plot関数で指定しなくても指定色を順に選んで描画してくれます。
グラデーション色は「事前定義されたカラーマップ」を使ったり、独自のRGB色を定義する事も可能です。
n = 100; d = 0.1;
x = repmat((0:d:2*pi)',1,n); % 0~2πの列をn行重ねた行列
y = sin(x) + (d:d:n*d); % 上記のsinを取りy軸方向にちょっとずつずらす
colororder(parula(n)); % 事前定義されたカラーマップ"parula”を使用する例
% colororder(repmat((d:d:n*d)'./(n*d),1,3)) % 独自に作るグラデーション色の例
plot(x,y,'LineWidth',2); % 線の太さ1だと隙間が出来る。太さ2だと綺麗になる
  2 Comments
Atsushi Ueno
Atsushi Ueno on 16 Mar 2022
Edited: Atsushi Ueno on 16 Mar 2022
for文で1ラインずつ描画する場合も同様です。下記の様にすれば上記回答と同様に動作します。
figure; hold on
colororder(parula(n));
for i = 1:size(x,2)
plot(x(:,i),y(:,i));
end
>colororder関数colororder(newcolors) は、現在の Figure での色の順序を設定します。
なので、colororder関数の実行後にfigureを作成するとデフォルトの色の順序(7色)に戻ります。
RU
RU on 17 Mar 2022
丁寧なご回答いただき誠にありがとうございます。
実際のfigureも載せていただきとても分かり易く参考になりました。
今後ともどうぞよろしくお願いいたします。

Sign in to comment.

More Answers (0)

Categories

Find more on 2 次元および 3 次元プロット in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!