質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

2492閲覧

matplotlibへのsubplot追加方法

asaliquid1011

総合スコア16

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/02/28 08:06

matplotlibでサブプロットを追加したいのですが、うまくいきません。改善方法を教えてもらえないでしょうか。
※Fig2を作成する前にFig1へサブプロットを追加すれば作れるのですが、他のFigを作成した後に追加する方法が知りたいです。

python

1t = np.arange(0.0, 5.0, 0.1) 2 3#Fig1 サブプロット作成 4y11 = np.exp(-t) * np.cos(2*np.pi*t) 5fig1=plt.figure() 6plt.subplot(121) 7plt.plot(t, y11, 'b-') 8 9#Fig2 サブプロット作成 10y21 = 2*t 11fig2=plt.figure() 12plt.subplot(121) 13plt.plot(t, y21, 'r-') 14 15#Fig1へサブプロット追加したい 16y12 = np.exp(-t) * np.cos(4*np.pi*t) 17fig1.add_subplot(122) 18plt.plot(t, y12, 'b-') 19

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

すでについているtiitoiさんとmagichanさんの回答は、十分エレガントな解決策になっています。

しかし、あえてPyplotインターフェースでやる。

python

1import numpy as np 2import matplotlib.pyplot as plt 3 4t = np.arange(0.0, 5.0, 0.1) 5 6#Fig1 サブプロット作成 7y11 = np.exp(-t) * np.cos(2*np.pi*t) 8fig1=plt.figure() 9plt.subplot(121) 10plt.plot(t, y11, 'b-') 11 12#Fig2 サブプロット作成 13y21 = 2*t 14fig2=plt.figure() 15plt.subplot(121) 16plt.plot(t, y21, 'r-') 17 18## 追加したもの 19# current figure切り替え 20plt.figure(fig1.number) 21 22#Fig1へサブプロット追加したい 23y12 = np.exp(-t) * np.cos(4*np.pi*t) 24fig1.add_subplot(122) 25plt.plot(t, y12, 'b-') 26 27plt.show()

できる根拠:

num : integer or string, optional, default: None

If not provided, a new figure will be created, and the figure number will be incremented. The figure objects holds this number in a number attribute. If num is provided, and a figure with this id already exists, make it active, and returns a reference to it. If this figure does not exists, create it and returns it. If num is a string, the window title will be set to this figure's num.

matplotlib.pyplot.figure — Matplotlib 3.1.2 documentation

投稿2020/02/28 20:35

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

MATLABスタイルのコーディングで今回のような複雑なことをやろうとするのは無理がありますので、オブジェクト指向スタイルのコーディングで行うと簡単に実現できます。

今回の場合は、Axes.plot() を使うとよいです。

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.plot.html

Python

1#Fig1へサブプロット追加したい 2y12 = np.exp(-t) * np.cos(4*np.pi*t) 3ax = fig1.add_subplot(122) 4ax.plot(t, y12, 'b-')

投稿2020/02/28 09:07

magichan

総合スコア15898

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

add_subplot() で作成した Axes を ax = fig.add_subplot() と変数に保存しておき、描画するときは、Axes の plot() を呼び出すように変更しましょう。

python

1import matplotlib.pyplot as plt 2import numpy as np 3 4t = np.arange(0.0, 5.0, 0.1) 5 6# Fig1 サブプロット作成 7y11 = np.exp(-t) * np.cos(2 * np.pi * t) 8 9fig1 = plt.figure() 10ax11 = fig1.add_subplot(121) 11ax11.plot(t, y11, "b-") 12 13# Fig2 サブプロット作成 14y21 = 2 * t 15fig2 = plt.figure() 16ax21 = fig2.add_subplot(121) 17ax21.plot(t, y21, "r-") 18 19# Fig1へサブプロット追加したい 20y12 = np.exp(-t) * np.cos(4 * np.pi * t) 21ax12 = fig1.add_subplot(122) 22ax12.plot(t, y12, "b-") 23 24plt.show()

イメージ説明

plt.subplots()、plt.figure() 以外の plt.<関数名> といった関数はステートフル インタフェースであるということを理解した上で使わないと、混乱の原因になるので、基本的には使わないほうがいいでしょう。

Python - matplotlibを用いた異なるウィンドウ間でのインタラクティブプロット|teratail

投稿2020/02/28 08:48

編集2020/02/28 09:14
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問