python:matplotlib:基本的な使い方
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python:matplotlib:基本的な使い方 [2018/01/20 23:17] – [プロット範囲の指定] koudai | python:matplotlib:基本的な使い方 [2021/06/27 22:04] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | x = np.arange(-np.pi, np.pi, 0.1) # xを-πからπまで0.1刻みで用意する | + | x = np.arange(-5, 5, 0.1) |
| - | y = np.sin(x) | + | y = np.sin(x) |
| plt.plot(x, y) # 横軸をxとして y=y(x) のグラフをプロットする | plt.plot(x, y) # 横軸をxとして y=y(x) のグラフをプロットする | ||
| Line 29: | Line 29: | ||
| {{: | {{: | ||
| - | 保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします(Ctrlキーを押しながらsを押すのでも可)。 | + | 保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします([Ctrl-s]も可)。 |
| - | あるいはplt.savefigを使うとファイルに直接出力できます。 | + | |
| + | ===== グラフの表示範囲の指定 ===== | ||
| + | |||
| + | プロットしたグラフを画面に表示する際、そのままだとすべてのデータが枠内に収まるように表示されます。 | ||
| + | 表示したい範囲を制限したい場合は次のようにします。 | ||
| <file python sin.py> | <file python sin.py> | ||
| Line 37: | Line 41: | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | x = np.arange(-np.pi, np.pi, 0.1) | + | x = np.arange(-5, 5, 0.1) |
| - | y = np.sin(x) | + | y1 = np.sin(x) |
| + | y2 = np.cos(x) | ||
| - | plt.plot(x, y) | + | plt.xlim(-np.pi, np.pi) # x軸は-πからπまで |
| - | plt.savefig("sin.png" | + | plt.ylim(-1.2, |
| + | |||
| + | plt.show() | ||
| </ | </ | ||
| - | 保存できる形式は | + | {{: |
| - | * emf | + | |
| - | * eps | + | |
| - | * jpeg | + | |
| - | * jpg | + | |
| - | + | ||
| - | * png | + | |
| - | * ps | + | |
| - | * raw | + | |
| - | * rgba | + | |
| - | * svg | + | |
| - | * svgz | + | |
| - | * tif | + | |
| - | * tiff | + | |
| - | です。 | + | |
| - | 拡張子により自動判別されます。 | + | |
| Line 67: | Line 61: | ||
| ついでにタイトルもつけます。 | ついでにタイトルもつけます。 | ||
| - | <file python | + | <file python> |
| import numpy as np | import numpy as np | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | x = np.arange(-np.pi, np.pi, 0.1) | + | x = np.arange(-5, 5, 0.1) |
| y = np.sin(x) | y = np.sin(x) | ||
| + | |||
| plt.title(" | plt.title(" | ||
| plt.xlabel(" | plt.xlabel(" | ||
| - | plt.ylabel(" | + | plt.ylabel(" |
| plt.plot(x, y) | plt.plot(x, y) | ||
| + | |||
| + | plt.xlim(-np.pi, | ||
| + | plt.ylim(-1.2, | ||
| + | |||
| plt.show() | plt.show() | ||
| </ | </ | ||
| - | {{: | + | {{: |
| Line 90: | Line 89: | ||
| どっちのグラフがどっちの関数かわからなくなるので凡例(legend)をつけます。 | どっちのグラフがどっちの関数かわからなくなるので凡例(legend)をつけます。 | ||
| - | <file python | + | <file python> |
| import numpy as np | import numpy as np | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | x = np.arange(-np.pi, np.pi, 0.1) | + | x = np.arange(-5, 5, 0.1) |
| y1 = np.sin(x) | y1 = np.sin(x) | ||
| y2 = np.cos(x) | y2 = np.cos(x) | ||
| Line 105: | Line 104: | ||
| plt.plot(x, y2, label=" | plt.plot(x, y2, label=" | ||
| plt.legend() | plt.legend() | ||
| + | |||
| + | plt.xlim(-np.pi, | ||
| + | plt.ylim(-1.2, | ||
| plt.show() | plt.show() | ||
| </ | </ | ||
| - | {{: | + | {{: |
| 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。 | 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。 | ||
| Line 116: | Line 118: | ||
| - | ===== プロット範囲の指定 ===== | ||
| - | matplotlibでは、そのままプロットするとすべてのデータが枠内に収まるようにプロットされます。 | ||
| - | プロットしたい範囲を制限する場合は次のようにします。 | ||
| - | <file python | + | ====== データのプロット ====== |
| + | |||
| + | 次に配列に格納されているデータをプロットしてみましょう。 | ||
| + | 以下では$y=x^2$のデータを用意しました。 | ||
| + | 配列のサイズはx成分とy成分で同じにします。 | ||
| + | |||
| + | <file python> | ||
| import numpy as np | import numpy as np | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | x = np.arange(-2.0 * np.pi, 2.0 * np.pi, 0.1) | + | x = [1,2,3, 4, 5, 6, 7, 8, 9, 10] # データのx成分 |
| - | y1 = np.sin(x) | + | y = [1, |
| - | y2 = np.cos(x) | + | |
| plt.title(" | plt.title(" | ||
| Line 133: | Line 137: | ||
| plt.ylabel(" | plt.ylabel(" | ||
| - | plt.plot(x, | + | plt.plot(x, |
| - | plt.plot(x, y2, label=" | + | |
| - | plt.legend() | + | |
| - | + | ||
| - | plt.xlim(-np.pi, | + | |
| - | plt.ylim(-1.2, | + | |
| plt.show() | plt.show() | ||
| </ | </ | ||
| - | {{: | + | {{: |
| - | + | ||
| - | + | ||
| - | + | ||
| - | ====== データのプロット ====== | + | |
| + | 各データは直線で結ばれます。 | ||
| + | もし点だけにしたいなら< | ||
python/matplotlib/基本的な使い方.1516457860.txt.gz · Last modified: 2021/06/27 21:59 (external edit)
