Takuya Miyashita
This site
Web
Start:
*図作成時のFigure,Axesインスタンス生成についての整理 [#c...
**はじめに [#o1839053]
図作成の事前作業にあたる Figure・Axes インスタンス生成に...
ここではそれぞれfig,axとして格納する.
インポートは下記の通り.~
#codeprettify(lang-python){{
import numpy as np
import matplotlib.pyplot as plt
}}
以下のことを頭に入れておくことで,理解がスムーズになると...
-Figure オブジェクトの中に Axes オブジェクトがある~
-Axes オブジェクトの中に Axis オブジェクトがある~
-plt.figure() や plt.subplots() は Figure (と Axes) イン...
-plt.xxx はカレントの Figure または Axes に対して実行され...
-fig.xxx, ax.xxx はカレントではなく具体的に Figure また...
なお,1つのウィンドウに1つの図しか配置しない場合において...
** fig, ax = plt.subplots() 方式 [#le832919]
Figure と Axes のインスタンスを同時に生成.~
#codeprettify(lang-python){{
fig, ax = plt.subplots(1,1)
ax.plot(np.random.rand(100))
}}
subplots の中の数字は row, col で,他の方法でやっているよ...
これでは111個の Axes インスタンスが生成されてしまう.~
図を2つ以上配置する場合は
#codeprettify(lang-python){{
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.plot(np.random.rand(100))
}}
#codeprettify(lang-python){{
fig, ax = plt.subplots(2,1)
ax[0].plot(np.random.rand(100)) % 2行2列の場合はax[0][0]....
}}
のように,個別に Axes を割り当てる方法と, Axes の集合を...
** plt.subplot() 方式 [#l5f2f325]
subplots と紛らわしいが,だいぶ違う.最も MATLAB に近い方...
#codeprettify(lang-python){{
plt.figure()
plt.subplot(211) # カレントの Figure オブジェクトを対象
plt.plot(np.random.rand(100)) # カレントの Axes オブジェ...
plt.subplot(212)
plt.plot(np.random.rand(100))
}}
最初の plt.figure() は書かなくても良い.figure がひとつも...
その都度アクティブな Figure オブジェクト,Axes オブジェク...
体裁を整える時になんだかんだで Axes オブジェクトのメソッ...
** fig = plt.figure() → ax = fig.add_subplot() 方式 [#pb3...
オブジェクト指向に沿った方法その2.
#codeprettify(lang-python){{
fig = plt.figure()
ax1 = fig.add_subplot(211) # figのメソッドを使用
ax1.plot(np.random.rand(100)) # axのメソッドを使用
ax2 = fig.add_subplot(212)
ax2.plot(np.random.rand(100))
}}
plt.subplots() と異なり,Axes のインスタンスを1つずつ生成...
オブジェクトを明示するため,生成すれば記述する順番はどうで...
比較的自由が利く.
#codeprettify(lang-python){{
# 上記と同じ
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(np.random.rand(100))
ax2.plot(np.random.rand(100))
}}
**その他 [#g08b3f30]
他にも,plt.subplot() から Axes インスタンスを生成してそ...
#codeprettify(lang-python){{
# 非推奨なやり方
fig = plt.figure()
ax = plt.subplot(111) # カレントの Figure オブジェクトか...
ax.plot(np.random.rand(100)) # ax のメソッドを使用
}}
また,下記のように fig, ax を取得することもできる.
#codeprettify(lang-python){{
fig = plt.gcf()
ax = plt.gca()
}}
ただし gcf や gca を使用する時点で,その前に取得すべき箇...
例外的に,関数の内部で取得するときや引数として放り込む時に...
***参考 [#z19e286f]
-[[matplotlib.pyplot.figure>https://matplotlib.org/api/_a...
-[[matplotlib.pyplot.subplots>https://matplotlib.org/api/...
-[[matplotlib.pyplot.subplot>https://matplotlib.org/api/_...
-[[matplotlib入門 りんごがでている>http://bicycle1885.hat...
-[[MATLABユーザのためのPythonグラフ作成ツールmatplotlib入...
-[[早く知っておきたかったmatplotlibの基礎知識、あるいは見...
End:
*図作成時のFigure,Axesインスタンス生成についての整理 [#c...
**はじめに [#o1839053]
図作成の事前作業にあたる Figure・Axes インスタンス生成に...
ここではそれぞれfig,axとして格納する.
インポートは下記の通り.~
#codeprettify(lang-python){{
import numpy as np
import matplotlib.pyplot as plt
}}
以下のことを頭に入れておくことで,理解がスムーズになると...
-Figure オブジェクトの中に Axes オブジェクトがある~
-Axes オブジェクトの中に Axis オブジェクトがある~
-plt.figure() や plt.subplots() は Figure (と Axes) イン...
-plt.xxx はカレントの Figure または Axes に対して実行され...
-fig.xxx, ax.xxx はカレントではなく具体的に Figure また...
なお,1つのウィンドウに1つの図しか配置しない場合において...
** fig, ax = plt.subplots() 方式 [#le832919]
Figure と Axes のインスタンスを同時に生成.~
#codeprettify(lang-python){{
fig, ax = plt.subplots(1,1)
ax.plot(np.random.rand(100))
}}
subplots の中の数字は row, col で,他の方法でやっているよ...
これでは111個の Axes インスタンスが生成されてしまう.~
図を2つ以上配置する場合は
#codeprettify(lang-python){{
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.plot(np.random.rand(100))
}}
#codeprettify(lang-python){{
fig, ax = plt.subplots(2,1)
ax[0].plot(np.random.rand(100)) % 2行2列の場合はax[0][0]....
}}
のように,個別に Axes を割り当てる方法と, Axes の集合を...
** plt.subplot() 方式 [#l5f2f325]
subplots と紛らわしいが,だいぶ違う.最も MATLAB に近い方...
#codeprettify(lang-python){{
plt.figure()
plt.subplot(211) # カレントの Figure オブジェクトを対象
plt.plot(np.random.rand(100)) # カレントの Axes オブジェ...
plt.subplot(212)
plt.plot(np.random.rand(100))
}}
最初の plt.figure() は書かなくても良い.figure がひとつも...
その都度アクティブな Figure オブジェクト,Axes オブジェク...
体裁を整える時になんだかんだで Axes オブジェクトのメソッ...
** fig = plt.figure() → ax = fig.add_subplot() 方式 [#pb3...
オブジェクト指向に沿った方法その2.
#codeprettify(lang-python){{
fig = plt.figure()
ax1 = fig.add_subplot(211) # figのメソッドを使用
ax1.plot(np.random.rand(100)) # axのメソッドを使用
ax2 = fig.add_subplot(212)
ax2.plot(np.random.rand(100))
}}
plt.subplots() と異なり,Axes のインスタンスを1つずつ生成...
オブジェクトを明示するため,生成すれば記述する順番はどうで...
比較的自由が利く.
#codeprettify(lang-python){{
# 上記と同じ
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(np.random.rand(100))
ax2.plot(np.random.rand(100))
}}
**その他 [#g08b3f30]
他にも,plt.subplot() から Axes インスタンスを生成してそ...
#codeprettify(lang-python){{
# 非推奨なやり方
fig = plt.figure()
ax = plt.subplot(111) # カレントの Figure オブジェクトか...
ax.plot(np.random.rand(100)) # ax のメソッドを使用
}}
また,下記のように fig, ax を取得することもできる.
#codeprettify(lang-python){{
fig = plt.gcf()
ax = plt.gca()
}}
ただし gcf や gca を使用する時点で,その前に取得すべき箇...
例外的に,関数の内部で取得するときや引数として放り込む時に...
***参考 [#z19e286f]
-[[matplotlib.pyplot.figure>https://matplotlib.org/api/_a...
-[[matplotlib.pyplot.subplots>https://matplotlib.org/api/...
-[[matplotlib.pyplot.subplot>https://matplotlib.org/api/_...
-[[matplotlib入門 りんごがでている>http://bicycle1885.hat...
-[[MATLABユーザのためのPythonグラフ作成ツールmatplotlib入...
-[[早く知っておきたかったmatplotlibの基礎知識、あるいは見...
Page:
Edit with a page name which already exists