Matplotlib-axes-class
Matplotlib-軸クラス
Axesオブジェクトは、データ空間を持つ画像の領域です。 特定のFigureには多くのAxesを含めることができますが、特定のAxesオブジェクトは1つのFigureにのみ含めることができます。 軸には2つ(3Dの場合は3つ)の軸オブジェクトが含まれます。 Axesクラスとそのメンバー関数は、OOインターフェイスを操作するための主要なエントリポイントです。
Axesオブジェクトは、add_axes()メソッドを呼び出すことでFigureに追加されます。 Axesオブジェクトを返し、すべての数量が図の幅と高さの分数である位置rect [left、bottom、width、height]に軸を追加します。
パラメータ
以下は、Axesクラスのパラメーターです-
- rect-[左、下、幅、高さ]量の4つの長さのシーケンス。
ax=fig.add_axes([0,0,1,1])
Axesクラスの次のメンバー関数は、プロットに異なる要素を追加します-
伝説
Axesクラスの* legend()*メソッドは、プロット図に凡例を追加します。 それは3つのパラメータを取ります-
ax.legend(handles, labels, loc)
ラベルは文字列のシーケンスであり、Line2DまたはPatchインスタンスのシーケンスを処理します。 locは、凡例の場所を指定する文字列または整数です。
Location string | Location code |
---|---|
Best | 0 |
upper right | 1 |
upper left | 2 |
lower left | 3 |
lower right | 4 |
Right | 5 |
Center left | 6 |
Center right | 7 |
lower center | 8 |
upper center | 9 |
Center | 10 |
axes.plot()
これは、1つの配列の値を別の配列の値を線またはマーカーとしてプロットするAxesクラスの基本的な方法です。 plot()メソッドには、ラインとマーカーの色、スタイル、サイズを指定するオプションのフォーマット文字列引数を含めることができます。
カラーコード
Character | Color |
---|---|
‘b’ | Blue |
‘g’ | Green |
‘r’ | Red |
‘b’ | Blue |
‘c’ | Cyan |
‘m’ | Magenta |
‘y’ | Yellow |
‘k’ | Black |
‘b’ | Blue |
‘w’ | White |
マーカーコード
Character | Description |
---|---|
‘.’ | Point marker |
‘o’ | Circle marker |
‘x’ | X marker |
‘D’ | Diamond marker |
‘H’ | Hexagon marker |
‘s’ | Square marker |
‘+’ | Plus marker |
線のスタイル
Character | Description |
---|---|
‘-‘ | Solid line |
‘—‘ | Dashed line |
‘-.’ | Dash-dot line |
‘:’ | Dotted line |
‘H’ | Hexagon marker |
次の例は、テレビとスマートフォンの広告費用と売上高を折れ線グラフの形式で示しています。 テレビを表す線は黄色と正方形のマーカーが付いた実線で、スマートフォンの線は緑色と円のマーカーが付いた破線です。
import matplotlib.pyplot as plt
y = [1, 4, 9, 16, 25,36,49, 64]
x1 = [1, 16, 30, 42,55, 68, 77,88]
x2 = [1,6,12,18,28, 40, 52, 65]
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
l1 = ax.plot(x1,y,'ys-') # solid line with yellow colour and square marker
l2 = ax.plot(x2,y,'go--') # dash line with green colour and circle marker
ax.legend(labels = ('tv', 'Smartphone'), loc = 'lower right') # legend placed at lower right
ax.set_title("Advertisement effect on sales")
ax.set_xlabel('medium')
ax.set_ylabel('sales')
plt.show()
上記のコード行が実行されると、次のプロットが生成されます-