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/20 22:18] koudai |
python:matplotlib:基本的な使い方 [2018/01/20 23:54] koudai [プロット範囲の指定] |
||
---|---|---|---|
Line 10: | Line 10: | ||
===== 初めてのプロット ===== | ===== 初めてのプロット ===== | ||
- | とりあえず何か書いてみましょう。 | + | とりあえずsin関数を書いてみましょう。 |
次のスクリプトを作成します。 | 次のスクリプトを作成します。 | ||
Line 33: | Line 33: | ||
あるいはplt.savefigを使うとファイルに直接出力できます。 | あるいはplt.savefigを使うとファイルに直接出力できます。 | ||
- | <file python | + | <file python> |
import numpy as np | import numpy as np | ||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
Line 41: | Line 41: | ||
plt.plot(x, y) | plt.plot(x, y) | ||
- | plt.savefig(" | + | plt.savefig(" |
</ | </ | ||
Line 67: | Line 67: | ||
ついでにタイトルもつけます。 | ついでにタイトルもつけます。 | ||
- | <file python | + | <file python> |
import numpy as np | import numpy as np | ||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
Line 73: | Line 73: | ||
x = np.arange(-np.pi, | x = np.arange(-np.pi, | ||
y = np.sin(x) | y = np.sin(x) | ||
- | plt.title(" | + | plt.title(" |
- | plt.xlabel(r"$x$" | + | plt.xlabel(" |
- | plt.ylabel(r"$\sin x$" | + | plt.ylabel(" |
plt.plot(x, y) | plt.plot(x, y) | ||
Line 81: | Line 81: | ||
</ | </ | ||
- | ここで<nowiki>r" | + | {{: |
- | これで | + | |
+ | |||
+ | ===== 複数のグラフ ===== | ||
+ | |||
+ | 複数のグラフを同時にプロットしたい場合を扱います。 | ||
+ | 例えばsin関数とcos関数を同時にプロットしたい場合は、次のようにします。 | ||
+ | どっちのグラフがどっちの関数かわからなくなるので凡例(legend)をつけます。 | ||
+ | |||
+ | <file python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-np.pi, | ||
+ | y1 = np.sin(x) | ||
+ | y2 = np.cos(x) | ||
+ | |||
+ | plt.title("graph" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y1, label="sin x" | ||
+ | plt.plot(x, y2, label=" | ||
+ | plt.legend() | ||
+ | |||
+ | plt.show() | ||
+ | </file> | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。 | ||
+ | 手動で設定する場合は、例えば左上の場合は <file python> | ||
+ | (右下の場合は lower right とします。他も同様) | ||
+ | |||
+ | |||
+ | ===== グラフの表示範囲の指定 ===== | ||
+ | |||
+ | プロットしたグラフを画面に表示する際、そのままだとすべてのデータが枠内に収まるように表示されます。 | ||
+ | 表示したい範囲を制限したい場合は次のようにします。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-2.0 * np.pi, 2.0 * np.pi, 0.1) # -2πから2πまでのxを用意 | ||
+ | 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() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
====== データのプロット ====== | ====== データのプロット ====== | ||
+ | 次に配列に格納されているデータをプロットしてみましょう。 | ||
+ | 以下では$y=x^2$のデータを用意しました。 | ||
+ | |||
+ | <file python> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = [1, | ||
+ | y = [1, | ||
+ | |||
+ | plt.title(" | ||
+ | plt.xlabel(" | ||
+ | plt.ylabel(" | ||
+ | |||
+ | plt.plot(x, y) | ||
+ | |||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | 各データは直線で結ばれます。 | ||
+ | もし点だけにしたいなら< | ||