彩色を施す

Last modified: 2018 年 12 月 30 日 03 時

白黒の図に色を入れる

主な使用関数: for, Listplot, Paramplot, Setcolor.

彩色を施す

下準備

通常の白黒のプロットに色を入れてみます。いつも通りプロットデータを計算しておいて、TeXファイルに吐き出す段階で色を指定する手順になります。ここでは、$\log (1+x)$ の $x=0$ におけるTaylor展開のプロットを例に色を付けてみます。

まずは、対数関数 $y=\log(1+x)$ と多項式関数 $\displaystyle y=\sum_{k=1}^n\frac{(-1)^{k+1}}{k}x^k$ のグラフのプロットデータを用意します。

epsilon=0.4;
xmin=-1-epsilon;
xmax=2+epsilon;
ymin=-2.5;
ymax=2.5;

Setwindow([xmin,xmax], [ymin,ymax]);

function f=f(x,n);  //第n項までのlog(1+x)のTaylor展開
  f=0;
  for i=1:n
    f=f+(-1)^(i+1)*x^i/i;
  end
endfunction

curve=Paramplot('[x, log(1+x)]', 'x=[-0.98,4]', 'N=300');

C=list();
N=5;
for i=1:N
  C($+1)=Paramplot('[t, f(t,i)]', 't=[xmin,xmax]', 'N=300');
end

T=Listplot([1,ymax], [-1,ymax], [-1,ymin], [1,ymin], [1,ymax]);
L1=Listplot([-1,ymax], [-1,ymin]);
L2=Listplot([1,ymax], [1,ymin]);

//Windisp(curve, C, T);

多項式関数のプロットデータのリストをCとおきます。また、長方形領域Tは $\log (1+x)$ のTaylor展開の収束半径 $-1< x \leq 1$ を表していて、この領域全体にも色を付ける予定です。

[このページのトップへ]

色を付ける

Openfile内でSetcolor(色名, 濃さ)と指定すると、そのあとに描画されるすべての線・領域は指定した色になります。ここで、色名はマニュアルに載っている色名で、濃さは0から1の間の数です。また、CMYKのベクトル[c,m,y,k]でも色が指定できます(Version 5.1.6aより)。

Setcolor('orange',1)でオレンジ色を指定してから、Shade(T, 0.08)で長方形領域Tを塗りつぶします。通常は黒色(濃さを変えれば、グレー)で塗りつぶされますが、直前にSetcolorを実行したのでオレンジ色になります。また、実行した順番通りに描画されるので、色の指定する順番に注意しましょう。加えて、Endpicture(1)で座標軸を描画するときは、一番最後にSetcolor('black',1)と戻しておかないと、座標軸の色が直近に指定された色になるのでやはり注意です。

Openfile('tex/color_sample.tex');  //color.sty必須
Beginpicture('1.5cm');
Setax(6,'O', 'nw');

Setcolor('orange', 1);
Shade(T, 0.08);

Setcolor('black', 1);
Dashline(L1, 0.5);
Drwline(L2, 1);

for i=1:N
  Setcolor([0,0.96,0.39,0], 0.1+(1-0.1)/(N+1)*i);  //wildstrawberry
  Drwline(C(i), 1.4);
end
Setcolor('black', 1);
Drwline(curve, 1);

Expr([2.2,log(2)], 'c', 'y=\log (1+x)');
Expr([1,0], 'ne', '1');
Expr([-1,0], 'nw', '-1');

Letter([xmax,f(xmax,1)], 'e', '\textcircled{\footnotesize 1}');
Letter([xmax,f(xmax,2)], 'e', '\textcircled{\footnotesize 2}');
Letter([1.95,f(1.95,3)], 'n', '\textcircled{\footnotesize 3}');
Letter([2.2,f(2.2,4)], 'e', '\textcircled{\footnotesize 4}');
Letter([1.675,f(1.675,5)], 'n', '\textcircled{\footnotesize 5}');
Letter([2.4,-1.4], 'c',..
 '\ovalbox{\textcircled{\footnotesize n} :..
    $\displaystyle y=\sum_{k=1}^n \frac{(-1)^{k+1}}{k}x^k$}');
//fancybox.styを使っています
Endpicture(1);
Closefile();

[このページのトップへ]

ソースコード

出力されたTeXファイルを利用するには、color.styが必要です。また、このサンプルコードでは、fancybox.styを使っています。

ソース(.sce)のダウンロード
epsilon=0.4;
xmin=-1-epsilon;
xmax=2+epsilon;
ymin=-2.5;
ymax=2.5;

Setwindow([xmin,xmax], [ymin,ymax]);

function f=f(x,n);
  f=0;
  for i=1:n
    f=f+(-1)^(i+1)*x^i/i;
  end
endfunction

curve=Paramplot('[x, log(1+x)]', 'x=[-0.98,4]', 'N=300');
C=list();

N=5;
for i=1:N
  C($+1)=Paramplot('[t, f(t,i)]', 't=[xmin,xmax]', 'N=300');
end

T=Listplot([1,ymax], [-1,ymax], [-1,ymin], [1,ymin], [1,ymax]);
L1=Listplot([-1,ymax], [-1,ymin]);
L2=Listplot([1,ymax], [1,ymin]);

//Windisp(curve, C, T);

Openfile('tex/color_sample.tex');
Beginpicture('1.5cm');
Setax(6,'O', 'nw');

Setcolor('orange', 1);
Shade(T, 0.08);

Setcolor('black', 1);
Dashline(L1, 0.5);
Drwline(L2, 1);

for i=1:N
  Setcolor([0,0.96,0.39,0], 0.1+(1-0.1)/(N+1)*i);  //wildstrawberry
  Drwline(C(i), 1.4);
end
Setcolor('black', 1);
Drwline(curve, 1);

Expr([2.2,log(2)], 'c', 'y=\log (1+x)');
Expr([1,0], 'ne', '1');
Expr([-1,0], 'nw', '-1');

Letter([xmax,f(xmax,1)], 'e', '\textcircled{\footnotesize 1}');
Letter([xmax,f(xmax,2)], 'e', '\textcircled{\footnotesize 2}');
Letter([1.95,f(1.95,3)], 'n', '\textcircled{\footnotesize 3}');
Letter([2.2,f(2.2,4)], 'e', '\textcircled{\footnotesize 4}');
Letter([1.675,f(1.675,5)], 'n', '\textcircled{\footnotesize 5}');
Letter([2.4,-1.4], 'c',..
 '\ovalbox{\textcircled{\footnotesize n} :..
    $\displaystyle y=\sum_{k=1}^n \frac{(-1)^{k+1}}{k}x^k$}');
//fancybox.styを使っています
Endpicture(1);
Closefile();

[このページのトップへ]