User Tools

Site Tools


python:matplotlib:基本的な使い方

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
python:matplotlib:基本的な使い方 [2018/01/20 21:52]
koudai
python:matplotlib:基本的な使い方 [2018/01/20 23:25]
koudai
Line 8: Line 8:
 三角関数を例に、関数のプロット方法と簡単な調節の仕方を紹介します。 三角関数を例に、関数のプロット方法と簡単な調節の仕方を紹介します。
  
-===== sin関数のプロット =====+===== 初めてのプロット =====
  
-matplotlibをインポートするこで、グラフの作成行えるよになります+りあえずsin関数書いてみましょう。
 次のスクリプトを作成します。 次のスクリプトを作成します。
 +
 +<file python sin.py>
 +import numpy as np
 +import matplotlib.pyplot as plt
 +
 +x = np.arange(-np.pi, np.pi, 0.1)  # xを-πからπまで0.1刻みで用意する
 +y = np.sin(x)                      # y = sin x
 +
 +plt.plot(x, y)   # 横軸をxとして y=y(x) のグラフをプロットする
 +plt.show()       # プロットしたグラフを画面に表示する
 +</file>
 +
 +それぞれのコードの意味はコメントを参照してください。
 +スクリプトができたら<code>$ python sin.py</code>を実行するとグラフが表示されます。
 +
 +{{:python:matplotlib:intro1.png?direct&400|}}
 +
 +保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします(Ctrlキーを押しながらsを押すのでも可)。
 +
 +あるいはplt.savefigを使うとファイルに直接出力できます。
  
 <file python sin.py> <file python sin.py>
Line 19: Line 39:
 x = np.arange(-np.pi, np.pi, 0.1) x = np.arange(-np.pi, np.pi, 0.1)
 y = np.sin(x) y = np.sin(x)
 +
 +plt.plot(x, y)
 +plt.savefig("sin.png"  # プロットしたグラフをファイルsin.pngに保存する
 +</file>
 +
 +保存できる形式は
 +  * emf
 +  * eps
 +  * jpeg
 +  * jpg
 +  * pdf
 +  * 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, np.pi, 0.1)
 +y = np.sin(x)
 +plt.title("graph"  # グラフのタイトル
 +plt.xlabel("x"     # x軸(横軸)のラベル
 +plt.ylabel("sin x" # y軸(縦軸)のラベル
  
 plt.plot(x, y) plt.plot(x, y)
Line 24: Line 81:
 </file> </file>
  
-できたら<code>python sin.py</code>を実行するとグラフ表示されます。+{{:python:matplotlib:intro2.png?direct&400|}} 
 + 
 + 
 +===== 複数のグラフ ===== 
 + 
 +複数のグラフを同時にプロットしい場合を扱います。 
 +例えばsin関数とcos関数を同時にプロットしたい場合は、次のようにします。 
 +どっちのグラフがどっちの関数かわかなくなるので凡例(legend)をつけます。 
 + 
 +<file python sin.py
 +import numpy as np 
 +import matplotlib.pyplot as plt 
 + 
 +x = np.arange(-np.pi, np.pi, 0.1) 
 +y1 = np.sin(x) 
 +y2 = np.cos(x) 
 + 
 +plt.title("graph"
 +plt.xlabel("x"
 +plt.ylabel("y"
 + 
 +plt.plot(x, y1, label="sin x"  # labelに凡例に表示する文字を指定 
 +plt.plot(x, y2, label="cos x") 
 +plt.legend()                     # 凡例を表示 
 + 
 +plt.show() 
 +</file> 
 + 
 +{{:python:matplotlib:intro3.png?direct&400|}} 
 + 
 +凡例の表示する位置は、matplotlibが適切な位置自動的に探します。 
 +手動で設定する場合は、例えば左上の場合は <file python>plt.legend(loc="upper left")</file>します。 
 +(右下の場合は lower right とします。他も同様) 
 + 
 + 
 +===== プロット範囲の指定 ===== 
 + 
 +matplotlibでは、そのままプロットするとすべてのデータ枠内に収まるようにプロットされます。 
 +そこから表示したい範囲を切り出す場合は次のようにします。 
 + 
 +<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("graph"
 +plt.xlabel("x"
 +plt.ylabel("y"
 + 
 +plt.plot(x, y1, label="sin x") 
 +plt.plot(x, y2, label="cos x") 
 +plt.legend() 
 + 
 +plt.xlim(-np.pi, np.pi)   # x軸は-πからπまで 
 +plt.ylim(-1.2,   1.2)     # y軸は-1.2から1.2まで 
 + 
 +plt.show() 
 +</file> 
 + 
 +{{:python:matplotlib:intro4.png?direct&400|}} 
  
  
python/matplotlib/基本的な使い方.txt · Last modified: 2021/06/27 22:04 (external edit)