This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
python:matplotlib:基本的な使い方 [2018/01/18 11:32] koudai |
python:matplotlib:基本的な使い方 [2018/01/21 01:29] koudai [初めてのプロット] |
||
---|---|---|---|
Line 2: | Line 2: | ||
matplotlibの必要最低限の使い方を解説します。 | matplotlibの必要最低限の使い方を解説します。 | ||
+ | |||
+ | |||
+ | ====== 関数のプロット ====== | ||
+ | |||
+ | 三角関数を例に、関数のプロット方法と簡単な調節の仕方を紹介します。 | ||
+ | |||
+ | ===== 初めてのプロット ===== | ||
+ | |||
+ | とりあえずsin関数を書いてみましょう。 | ||
+ | 次のスクリプトを作成します。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-5, | ||
+ | y = np.sin(x) | ||
+ | |||
+ | plt.plot(x, y) # 横軸をxとして y=y(x) のグラフをプロットする | ||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | それぞれのコードの意味はコメントを参照してください。 | ||
+ | スクリプトができたら< | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします(Ctrlキーを押しながらsを押すのでも可)。 | ||
+ | |||
+ | |||
+ | ===== グラフの表示範囲の指定 ===== | ||
+ | |||
+ | プロットしたグラフを画面に表示する際、そのままだとすべてのデータが枠内に収まるように表示されます。 | ||
+ | 表示したい範囲を制限したい場合は次のようにします。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-5, | ||
+ | y1 = np.sin(x) | ||
+ | y2 = np.cos(x) | ||
+ | |||
+ | plt.title(" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y1, label=" | ||
+ | plt.plot(x, y2, label=" | ||
+ | plt.legend() | ||
+ | |||
+ | plt.xlim(-np.pi, | ||
+ | plt.ylim(-1.2, | ||
+ | |||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== ラベルの作成 ===== | ||
+ | |||
+ | このままだと縦軸と横軸がなんなのかわからないので、軸にラベルをつけます。 | ||
+ | ついでにタイトルもつけます。 | ||
+ | |||
+ | <file python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-5, | ||
+ | y = np.sin(x) | ||
+ | plt.title(" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y) | ||
+ | |||
+ | plt.xlim(-np.pi, | ||
+ | plt.ylim(-1.2, | ||
+ | |||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | ===== 複数のグラフ ===== | ||
+ | |||
+ | 複数のグラフを同時にプロットしたい場合を扱います。 | ||
+ | 例えばsin関数とcos関数を同時にプロットしたい場合は、次のようにします。 | ||
+ | どっちのグラフがどっちの関数かわからなくなるので凡例(legend)をつけます。 | ||
+ | |||
+ | <file python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-5, | ||
+ | y1 = np.sin(x) | ||
+ | y2 = np.cos(x) | ||
+ | |||
+ | plt.title(" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y1, label=" | ||
+ | plt.plot(x, y2, label=" | ||
+ | plt.legend() | ||
+ | |||
+ | plt.xlim(-np.pi, | ||
+ | plt.ylim(-1.2, | ||
+ | |||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。 | ||
+ | 手動で設定する場合は、例えば左上の場合は <file python> | ||
+ | (右下の場合は lower right とします。他も同様) | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ====== データのプロット ====== | ||
+ | |||
+ | 次に配列に格納されているデータをプロットしてみましょう。 | ||
+ | 以下では$y=x^2$のデータを用意しました。 | ||
+ | 配列のサイズはx成分とy成分で同じにします。 | ||
+ | |||
+ | <file python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = [1,2,3, 4, 5, 6, 7, 8, 9, 10] # データのx成分 | ||
+ | y = [1, | ||
+ | |||
+ | plt.title(" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y) | ||
+ | |||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 各データは直線で結ばれます。 | ||
+ | もし点だけにしたいなら< | ||
+ | |||
+ |