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 23:54] koudai [プロット範囲の指定] |
python:matplotlib:基本的な使い方 [2018/01/21 00:30] koudai [ラベルの作成] |
||
---|---|---|---|
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 37: | Line 37: | ||
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) | ||
Line 59: | Line 59: | ||
* tiff | * tiff | ||
です。 | です。 | ||
- | 拡張子により自動判別されます。 | + | ファイルの種類は拡張子により自動判別されます。 |
+ | |||
+ | |||
+ | ===== グラフの表示範囲の指定 ===== | ||
+ | |||
+ | プロットしたグラフを画面に表示する際、そのままだとすべてのデータが枠内に収まるように表示されます。 | ||
+ | 表示したい範囲を制限したい場合は次のようにします。 | ||
+ | |||
+ | <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() | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
Line 71: | Line 103: | ||
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 94: | Line 130: | ||
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 141: | ||
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 115: | Line 154: | ||
(右下の場合は lower right とします。他も同様) | (右下の場合は 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() | ||
- | </ | ||
- | |||
- | {{: | ||
Line 151: | Line 161: | ||
次に配列に格納されているデータをプロットしてみましょう。 | 次に配列に格納されているデータをプロットしてみましょう。 | ||
以下では$y=x^2$のデータを用意しました。 | 以下では$y=x^2$のデータを用意しました。 | ||
+ | 配列のサイズはx成分とy成分で同じにします。 | ||
<file python> | <file python> |