16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matplotlibのsubplotsをfor文で使うと便利

Last updated at Posted at 2019-12-26

subplot と subplots を間違えないように気をつける

subplotsをfor文で使うと便利

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
%matplotlib inline

# データを用意
t = np.linspace(-np.pi, np.pi, 1000)
x1 = np.sin(2*t)
x2 = np.cos(2*t)
x3 = np.tan(2*t)
x4 = 1 / x1
x5 = 1 / x2
x6 = 1 / x3
  • (例1)6つのデータを2行3列でプロット
fig, axes = plt.subplots(2, 3, figsize=(16,12))
l = [[x1, x2, x3], [x4, x5, x6]]
for i in range(2):
    for j in range(3):
        axes[i][j].plot(t, l[i][j])
        axes[i][j].set_xlim(-np.pi, np.pi)
        axes[i][j].set_ylim(-1, 1)

tmp2.png

  • (例2)6つのデータを3行2列でプロット
fig, axes = plt.subplots(3, 2, figsize=(8,12))
l = [[x1, x2], [x3, x4], [x5, x6]]
for i in range(3):
    for j in range(2):
        axes[i][j].plot(t, l[i][j], color='r')
        axes[i][j].set_xlim(-np.pi, np.pi)
        axes[i][j].set_ylim(-1, 1)

tmp.png

簡単に解説

  • subplotsの第一,二引数はグラフの行,列
  • figsizeはグラフ一つあたりのサイズ(インチ)
  • axesとl(可視化したいデータのリスト)をスライス操作の感覚で使う
16
18
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
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?