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 21:42] koudai |
python:matplotlib:基本的な使い方 [2018/01/20 22:18] koudai |
||
---|---|---|---|
Line 3: | Line 3: | ||
matplotlibの必要最低限の使い方を解説します。 | matplotlibの必要最低限の使い方を解説します。 | ||
- | {{: | + | |
+ | ====== 関数のプロット ====== | ||
+ | |||
+ | 三角関数を例に、関数のプロット方法と簡単な調節の仕方を紹介します。 | ||
+ | |||
+ | ===== 初めてのプロット ===== | ||
+ | |||
+ | とりあえず何か書いてみましょう。 | ||
+ | 次のスクリプトを作成します。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-np.pi, | ||
+ | y = np.sin(x) | ||
+ | |||
+ | plt.plot(x, y) # 横軸をxとして y=y(x) のグラフをプロットする | ||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | それぞれのコードの意味はコメントを参照してください。 | ||
+ | スクリプトができたら< | ||
+ | |||
+ | {{: | ||
+ | |||
+ | 保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします(Ctrlキーを押しながらsを押すのでも可)。 | ||
+ | |||
+ | あるいはplt.savefigを使うとファイルに直接出力できます。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-np.pi, | ||
+ | y = np.sin(x) | ||
+ | |||
+ | plt.plot(x, y) | ||
+ | plt.savefig(" | ||
+ | </ | ||
+ | |||
+ | 保存できる形式は | ||
+ | * emf | ||
+ | * eps | ||
+ | * jpeg | ||
+ | * jpg | ||
+ | |||
+ | * png | ||
+ | * ps | ||
+ | * raw | ||
+ | * rgba | ||
+ | * svg | ||
+ | * svgz | ||
+ | * tif | ||
+ | * tiff | ||
+ | です。 | ||
+ | 拡張子により自動判別されます。 | ||
+ | |||
+ | |||
+ | ===== ラベルの作成 ===== | ||
+ | |||
+ | このままだと縦軸と横軸がなんなのかわからないので、軸にラベルをつけます。 | ||
+ | ついでにタイトルもつけます。 | ||
+ | |||
+ | <file python sin.py> | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | x = np.arange(-np.pi, | ||
+ | y = np.sin(x) | ||
+ | plt.title(" | ||
+ | plt.xlabel(r" | ||
+ | plt.ylabel(r" | ||
+ | |||
+ | plt.plot(x, y) | ||
+ | plt.show() | ||
+ | </ | ||
+ | |||
+ | ここで< | ||
+ | これで | ||
+ | |||
+ | ====== データのプロット ====== | ||
+ | |||
+ | |||
+ |