LoginSignup
1
2

More than 1 year has passed since last update.

グラフレイアウトの操作について(備忘録)

Last updated at Posted at 2021-08-29

Pythonを使ったデータ可視化時のレイアウトについての備忘録です。

コード実行の際には下記ライブラリを事前にインポート

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

figureとaxes

グラフ描画領域の概念として、figureaxesがある。
figureは、イメージとしては大きな画用紙(描画領域全体)
axesは、画用紙の中に描かれたグラフ描画領域
image.png

figureの作成

matplotlib.pyplot.figure関数で作成する。

# figureの作成
plt.figure(figsize=(5,10))
引数 説明
figsize figureのサイズ(横幅, 高さ)
dpi 解像度
facecolor 背景色
linewidth 外枠の太さ
edgecolor 枠の色

figureの中にグラフを1つのみ描画する場合

axesは指定する必要なし。

# データの作成
x = np.array([1,2,3,4,5,6,7,8,9,10])
y1 = x #1次関数
y2 = np.exp(x) #指数関数

# 1つ目のfigure
plt.figure(figsize=(3,3))
plt.plot(x,y1)

# 2つ目のfigure
plt.figure(figsize=(3,3))
plt.plot(x,y2)

plt.show()

image.pngimage.png
※実際は、上記のグラフが縦に並びます。

axexの作成(1つのfigureに複数のグラフを描画する場合)

figureを用意して、その中にaxesを作成する方法。
まず、plt.figure()関数でキャンバスを用意する。次にplt.subplot()関数でaxesを作成
plt.subplot()関数では、引数に行数,列数,描画位置を指定することで、キャンパスが行数×列数のグリッドに分割されたとした際の、指定した描画位置にaxesオブジェクトを作成することができる。

# figureを2行2列に分割されたとした場合の3の位置にaxesオブジェクトを作成
plt.subplot(2,2,3)

とした場合、
image.png
の3の位置にaxesオブジェクトが作成される。

x = np.array([1,2,3,4,5,6,7,8,9,10])
y1 = x
y2 = np.exp(x)
y3 = x ** 2
y4 = x ** 3

# figureの作成
plt.figure(figsize=[10,5])
# axesの作成(2行2列に分割した場合の左上を指定)
plt.subplot(2,2,1)
plt.plot(x,y1)

# axesの作成(2行2列に分割した場合の右上を指定)
plt.subplot(2,2,2)
plt.plot(x,y2)

# axesの作成(2行2列に分割した場合の左下を指定)
plt.subplot(2,2,3)
plt.plot(x,y3)

# axesの作成(2行2列に分割した場合の右下を指定)
plt.subplot(2,2,4)
plt.plot(x,y4)

plt.show()

image.png

figureオブジェクトとaxesオブジェクトの作成と操作

前述の操作の別の方法

figureオブジェクトの作成 ※これまでと同様のfigure関数です。

fig = plt.figure(figsize=(4,8))

axesオブジェクトをfigureオブジェクト内に作成(追加)する。
figureオブジェクト.add_subplot(行,列,配置番号)

# 2行2列とした場合の、
ax1 = fig.add_subplot(2,2,1) # 左上
ax2 = fig.add_subplot(2,2,2) # 右上
ax3 = fig.add_subplot(2,2,3) # 左下
ax4 = fig.add_subplot(2,2,4) # 右下

グラフを描画する場合は、axesオブジェクトを使ってグラフを描画する。

ax1.plot(x, y1)
ax2.plot(x, y2)
ax3.plot(x, y3)
ax4.plot(x, y4)

まとめると・・・

x = np.array([1,2,3,4,5,6,7,8,9,10])
y1 = x
y2 = np.exp(x)
y3 = x ** 2
y4 = x ** 3

# figure作成
fig = plt.figure(figsize=(10,5))

# axes作成(追加)
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

# グラフ描画
ax1.plot(x, y1)
ax2.plot(x, y2)
ax3.plot(x, y3)
ax4.plot(x, y4)

image.png

figureオブジェクトとaxesオブジェクトを一気に作成する

何行何列にグラフを配置するかを事前に指定して、figureとaxesを同時に作成する方法
fig, ax = plt.subplots(行数, 列数, figsize=(横幅, 高さ))
axはリストとなっている為、

axesオブジェクトが代入された変数[行インデックス, 列インデックス]として対象のグラフ領域を指定します。

x = np.array([1,2,3,4,5,6,7,8,9,10])
y1 = x
y2 = np.exp(x)
y3 = x ** 2
y4 = x ** 3

# figureとaxesの作成
fig, ax = plt.subplots(2,2,figsize=(10,5))

# グラフの作成
ax[0,0].plot(x, y1)
ax[0,1].plot(x, y2)
ax[1,0].plot(x, y3)
ax[1,1].plot(x, y4)

plt.show()

image.png

おしまい

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2