Numpy-matplotlib

提供:Dev Guides
移動先:案内検索

NumPy-Matplotlib

Matplotlibは、Python用のプロットライブラリです。 NumPyと一緒に使用して、MatLabの効果的なオープンソースの代替となる環境を提供します。 PyQtやwxPythonなどのグラフィックツールキットでも使用できます。

Matplotlibモジュールは、John Dによって最初に作成されました。 ハンター。 2012年以来、Michael Droettboomが主な開発者です。 現在、Matplotlib ver。 1.5.1は安定版です。 このパッケージは、バイナリ配布およびhttp://www.matplotlib.org [www.matplotlib.org]のソースコード形式で入手できます。

従来、パッケージは次のステートメントを追加することによりPythonスクリプトにインポートされます-

from matplotlib import pyplot as plt

ここで、* pyplot()はmatplotlibライブラリの最も重要な関数であり、2Dデータをプロットするために使用されます。 次のスクリプトは方程式をプロットします *y = 2x + 5

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y)
plt.show()

ndarrayオブジェクトxは、* x軸*の値として np.arange()function から作成されます。 * y軸*の対応する値は、別の ndarrayオブジェクトy に保存されます。 これらの値は、matplotlibパッケージのpyplotサブモジュールの* plot()*関数を使用してプロットされます。

グラフィック表示は、* show()*関数によって表示されます。

上記のコードは、次の出力を生成する必要があります-

Matplotlibデモ

線形グラフの代わりに、* plot()*関数にフォーマット文字列を追加することにより、値を個別に表示できます。 次のフォーマット文字を使用できます。

Sr.No. Character & Description
1

'-'

実線スタイル

2

'--'

破線のスタイル

3

'-.'

一点鎖線スタイル

4

':'

点線スタイル

5

'.'

ポイントマーカー

6

','

ピクセルマーカー

7

'o'

サークルマーカー

8

'v'

Triangle_downマーカー

9

'^'

Triangle_upマーカー

10

'<'

Triangle_leftマーカー

11

'>'

Triangle_rightマーカー

12

'1'

Tri_downマーカー

13

'2'

トライアップマーカー

14

'3'

Tri_leftマーカー

15

'4'

Tri_rightマーカー

16

's'

スクエアマーカー

17

'p'

五角形マーカー

18

'''*

スターマーカー

19

'h'

Hexagon1マーカー

20

'H'

Hexagon2マーカー

21

'+'

プラスマーカー

22

'x'

Xマーカー

23

'D'

ダイヤモンドマーカー

24

'd'

Thin_diamondマーカー

25 *'

'*

Vlineマーカー

26

次の色の略語も定義されています。

Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White

上記の例の線の代わりに、点を表す円を表示するには、plot()関数のフォーマット文字列として*“ ob” *を使用します。

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(1,11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.show()

上記のコードは、次の出力を生成する必要があります-

色の略語

正弦波プロット

次のスクリプトは、matplotlibを使用して*正弦波プロット*を生成します。

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave form")

# Plot the points using matplotlib
plt.plot(x, y)
plt.show()

サイン波

subplot()

subplot()関数を使用すると、同じ図にさまざまなものをプロットできます。 次のスクリプトでは、*サイン*および*コサイン値*がプロットされています。

import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)

# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')

# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')

# Show the figure.
plt.show()

上記のコードは、次の出力を生成する必要があります-

サブプロット

バー()

  • pyplotサブモジュール*は、棒グラフを生成する* bar()関数を提供します。 次の例では、2組の *x および y 配列の棒グラフを作成します。

from matplotlib import pyplot as plt
x = [5,8,10]
y = [12,16,6]

x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = 'center')
plt.bar(x2, y2, color = 'g', align = 'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')

plt.show()

このコードは、次の出力を生成する必要があります-

棒グラフ