科学の箱

科学・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:tensorf …

no image

wordpress-xmlrpc

WordPressへPythonから投稿してみる。 まずは設定から。説明はこちらにある。 https://python-wordpress-xmlrpc.readthedocs.io/en/lates …

no image

生成した配列をグラフで確認

生成した配列を可視化するためにはmatplotlibが利用できる。 ここでは簡単に可視化するための使い方を見てみる。 まずはnumpyとmatplolibモジュールを読み込む。以下ではnumpyはnp …

no image

数字、計算、文字列を画面出力

Hello Worldができたので、いろいろな出力を試してみる。 出力はprint関数を使えばよい。 まずは数値から試してみる。数値は文字列と違い引用符で囲む必要はない。画面に直接表示するので変数は使 …

no image

python data scientist bootcamp

pythonでdata分析をしたいと思ったが学校に通う時間もお金もない。udemyで検索したところ英語版であるが1400円!でコースが見つかったのでこちらを受けることにした。 https://www. …

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

side bar top



アーカイブ

カテゴリー