科学の箱

科学・IT・登山の話題

Python

複数のグラフを表示する方法

投稿日:

ここでは以下を説明する。

  • 複数のグラフを表示する2つの方法
  • サブプロットのグラフを整形

複数のグラフを表示する

複数のグラフを表示するためには二通りの方法がある。

  1. subplots()を使ってあらかじめエリアを作成
  2. subplot()でグラフを追加
  3. サブプロットのグラフを整形-subplot()

subplots()を使ってあらかじめエリアを作成

matplotlib.pyplot.subplots — Matplotlib 3.1.1 documentation

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None,
  • nrows, ncolsにグリッドの行数、列数を指定する。例えばnrow2=2, ncols=2ならグリッド数は2×2で4になる。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 10, 100)

fig, ax = plt.subplots(2)
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x))

 

subplot()でグラフを追加

subplot(nrows, ncols, index, **kwargs)
  • nrows, ncols, indexにそれぞれ、サブプロットの場所を示す、行、列、インデックスを指定する。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 10, 100)

fig = plt.figure()
plt.subplot(2,1,1)
plt.plot(x, np.sin(x))
plt.subplot(2,1,2)
plt.plot(x, np.cos(x))

 

サブプロットのグラフを整形

サブプロットのグラフを整形-subplots()

subplots()でグラフ描画エリアを作成し、サブププロットにグラフを描画することはできた。このグラフを整形する手順を下記に示す。

この手順はsubplotsで戻されたaxes.Axesを利用する。

axes.Axes — Matplotlib 3.1.1 documentation

グラフの整形はタイトル、ラベル等はAxis Labels, title, and legend

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 10, 100)

fig, ax = plt.subplots(2)
ax[0].plot(x, np.sin(x))
ax[0].set_title("Sin(X)")
ax[0].set_xlabel("Subplot 1 - x")
ax[0].set_ylabel("Subplot 1 - y")


ax[1].plot(x, np.cos(x))
ax[1].set_title("Cos(X)")
ax[1].set_xlabel("Subplot 2 - x")
ax[1].set_ylabel("Subplot 2 - y")

さて上記のグラフでは2つ目のグラフのタイトルが一つ目のグラフのx軸ラベルとオーバーラップしている。そのためx軸ラベルは表示されていない。このような場合にはpyplot.tight_layoutを使う。

Tight Layout guide — Matplotlib 3.1.1 documentation

 

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 10, 100)

fig, ax = plt.subplots(2)
ax[0].plot(x, np.sin(x))
ax[0].set_title("Sin(X)")
ax[0].set_xlabel("Subplot 1 - x")
ax[0].set_ylabel("Subplot 1 - y")


ax[1].plot(x, np.cos(x))
ax[1].set_title("Cos(X)")
ax[1].set_xlabel("Subplot 2 - x")
ax[1].set_ylabel("Subplot 2 - y")

# こちらを追加
plt.tight_layout()

 

サブプロットのグラフを整形-subplot()

subplot()の場合にはサブプロットを選択後にpyplotを利用してグラフを整形する。オーバーラップは発生するのでpyplot.tight_layout()は必ずよびだす。

 

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 10, 100)

fig = plt.figure()
plt.subplot(2,1,1)
plt.title("Sin(X)")
plt.xlabel("Subplot 1 - x")
plt.ylabel("Subplot 1 - y")
plt.plot(x, np.sin(x))
plt.tight_layout()

plt.subplot(2,1,2)
plt.plot(x, np.cos(x))
plt.title("Cos(X)")
plt.xlabel("Subplot 2 - x")
plt.ylabel("Subplot 2 - y")
plt.plot(x, np.sin(x))
plt.tight_layout()

 

 

メタ情報

inarticle



メタ情報

inarticle



-Python
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

scikit-learnで適切なアルゴリズムを選択するためのチートシート

  http://scikit-learn.org/stable/tutorial/machine_learning_map/index.html Related posts:K近傍法でデー …

no image

空の配列を生成

numpy.empty()を使うと要素が初期化されていない配列を生成できる。要素に入る値はその時により変わるので必ず初期化後には明示的に値を設定する必要がある。 numpy.empty print(n …

no image

numpy.linspace()を使って等差数列を生成する

一次関数と等差数列 一次関数をテストするときに必須になるのが等差数列。等差数列とは要素と要素の間の差が等しいもの。 例えば1, 2, 3, 4, 5, 6は等差が1の数列である。等差が2になると、1, …

no image

cp932 error

pythonで入力ファイルにおける文字コードが正しく認識されない場合、以下のようなエラーがでる。 UnicodeDecodeError: ‘cp932’ codec can&# …

no image

errorbarで誤差棒付きグラフの作成

概要 学習モデルのグラフは誤差を含んでいる。今2軸(xおよびy)をとり、以下のようなモデルに近似したとする。実際のデータは誤差がある。この誤差を表示できるのがerrorbarである。 ドキュメント h …

2019年9月
« 8月   10月 »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

side bar top



アーカイブ

カテゴリー