9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MATLABでグラフの色や太さを簡潔に設定する方法

Posted at

概要

  • MATLABでグラフの色や太さを設定する時,"Color", "r""LineWidth", 1.0などのパラメータをplotに与えるが,色や太さを細かく設定しようとすると長くなりがちである.
  • さらに,色や太さの設定をコード内で使いまわすことができない.
  • cell配列を使えば,簡潔に表記できる上に再利用も行いやすいため,本記事ではその方法を紹介する.

サンプルコード

style1 = {'Color',"r","LineWidth",3.0};
style2 = {'Color',"g","LineWidth",2.0};
style3 = {'Color',"b","LineWidth",1.0};

x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = 2*sin(x);
y3 = 3*sin(x);

plot(x,y1,style1{:});
hold on
plot(x,y2,style2{:});
hold on
plot(x,y3,style3{:});
  • style1style2のように,cell配列内に色や太さを指定するためのパラメータをいつも通り書く.
  • plotに渡す際は,style1{:}style2{:}のように表記する.
  • cell配列はcell{:}とすることで,配列の各要素を展開して関数に渡すことができる.
9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?