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 23:44]
koudai
python:matplotlib:基本的な使い方 [2018/01/22 20:13]
koudai [グラフの表示範囲の指定]
Line 17: Line 17:
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
  
-x = np.arange(-np.pinp.pi, 0.1)  # xを-πからπまで0.1刻みで用意する +x = np.arange(-55, 0.1)   # xを-5から5まで0.1刻みで用意する 
-y = np.sin(x)                      # y = sin x+y = np.sin(x)               # y = sin x
  
 plt.plot(x, y)   # 横軸をxとして y=y(x) のグラフをプロットする plt.plot(x, y)   # 横軸をxとして y=y(x) のグラフをプロットする
Line 29: Line 29:
 {{:python:matplotlib:intro1.png?direct&400|}} {{:python:matplotlib:intro1.png?direct&400|}}
  
-保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします(Ctrlキーを押しながらsを押すのでも可)。+保存するにはグラフが表示されたウィンドウの上の方にあるフロッピーディスクのマークをクリックします([Ctrl-s]も可)。
  
-あるいはplt.savefigを使うとファイルに直接出力できます。 
  
-<file python>+===== グラフの表示範囲の指定 ===== 
 + 
 +プロットしたグラフを画面に表示する際、そのままだとすべてのデータが枠内に収まるように表示されます。 
 +表示したい範囲を制限したい場合は次のようにします。 
 + 
 +<file python sin.py>
 import numpy as np import numpy as np
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
  
-x = np.arange(-np.pinp.pi, 0.1) +x = np.arange(-55, 0.1)  
-= np.sin(x)+y1 = np.sin(x) 
 +y2 = np.cos(x) 
 + 
 +plt.xlim(-np.pi, np.pi)   # x軸は-πからπまで 
 +plt.ylim(-1.2,   1.2)     # y軸は-1.2から1.2まで
  
-plt.plot(x, y) +plt.show()
-plt.savefig("sin.png"  # プロットしたグラフをファイルsin.pngに保存する+
 </file> </file>
  
-保存できる形式は +{{:python:matplotlib:intro2.png?direct&400|}} 
-  * emf + 
-  * eps +
-  * jpeg +
-  * jpg +
-  * pdf +
-  * png +
-  * ps +
-  * raw +
-  * rgba +
-  * svg +
-  * svgz +
-  * tif +
-  * tiff +
-です。 +
-拡張子により自動判別されます。+
  
  
Line 71: Line 65:
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
  
-x = np.arange(-np.pinp.pi, 0.1)+x = np.arange(-55, 0.1)
 y = np.sin(x) y = np.sin(x)
 plt.title("graph"  # グラフのタイトル plt.title("graph"  # グラフのタイトル
 plt.xlabel("x"     # x軸(横軸)のラベル plt.xlabel("x"     # x軸(横軸)のラベル
-plt.ylabel("sin x" # y軸(縦軸)のラベル+plt.ylabel("y"     # y軸(縦軸)のラベル
  
 plt.plot(x, y) plt.plot(x, y)
 +
 +plt.xlim(-np.pi, np.pi)
 +plt.ylim(-1.2,   1.2)
 +
 plt.show() plt.show()
 </file> </file>
  
-{{:python:matplotlib:intro2.png?direct&400|}}+{{:python:matplotlib:intro3.png?direct&400|}}
  
  
Line 94: Line 92:
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
  
-x = np.arange(-np.pinp.pi, 0.1)+x = np.arange(-55, 0.1)
 y1 = np.sin(x) y1 = np.sin(x)
 y2 = np.cos(x) y2 = np.cos(x)
Line 105: Line 103:
 plt.plot(x, y2, label="cos x") plt.plot(x, y2, label="cos x")
 plt.legend()                     # 凡例を表示 plt.legend()                     # 凡例を表示
 +
 +plt.xlim(-np.pi, np.pi)
 +plt.ylim(-1.2,   1.2)
  
 plt.show() plt.show()
 </file> </file>
  
-{{:python:matplotlib:intro3.png?direct&400|}}+{{:python:matplotlib:intro4.png?direct&400|}}
  
 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。 凡例の表示する位置は、matplotlibが適切な位置を自動的に探します。
Line 115: Line 116:
 (右下の場合は lower right とします。他も同様) (右下の場合は 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|}} 
  
  
Line 151: Line 123:
 次に配列に格納されているデータをプロットしてみましょう。 次に配列に格納されているデータをプロットしてみましょう。
 以下では$y=x^2$のデータを用意しました。 以下では$y=x^2$のデータを用意しました。
 +配列のサイズはx成分とy成分で同じにします。
  
 <file python> <file python>
Line 156: Line 129:
 import matplotlib.pyplot as plt import matplotlib.pyplot as plt
  
-x = [1,2,3,4,5,6,7,8,9,10]         # データのx成分 +x = [1,2,3, 4, 5, 6, 7, 8, 9, 10]   # データのx成分 
-y = [1,4,9,16,25,36,49,64,81,100]  # データのy成分+y = [1,4,9,16,25,36,49,64,81,100]   # データのy成分
  
 plt.title("graph") plt.title("graph")
Line 163: Line 136:
 plt.ylabel("y") plt.ylabel("y")
  
-plt.plot(x, y, label="sin x")+plt.plot(x, y)
  
 plt.show() plt.show()
-</pyrhon>+</file>
  
 {{:python:matplotlib:intro5.png?direct&400|}} {{:python:matplotlib:intro5.png?direct&400|}}
  
 各データは直線で結ばれます。 各データは直線で結ばれます。
-もし点だけにしたいなら<file python>plt.plot(x, y, label="sin x", "o")</file>とします(oは丸い点の意味です。その他については別のページで解説します)。+もし点だけにしたいなら<file python>plt.plot(x, y, "o")</file>とします(oは丸い点の意味です。その他については別のページで解説します)。
  
  
python/matplotlib/基本的な使い方.txt · Last modified: 2021/06/27 22:04 (external edit)