2017-12-12 8 views
1

私は、3つのサブプロットでこのグラフを作成していますが、それぞれのインセット軸が必要ですが、コードでは最後のサブプロットのインセット軸のみが作成されます。これで私を助けてください。ありがとう。複数のサブプロットを持つときmatplotlibのInsetPositionで問題が発生する

import pylab as pl 
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition 

def test(ax_main, pos, Data): 
    ax2 = pl.axes([0, 0, 1, 1]) 
    ip = InsetPosition(ax_main, pos) 
    ax2.set_axes_locator(ip) 
    pl.hist(Data) 

for i in range(3): 
    ax=pl.subplot(1,3,i+1) 
    Data = pl.rand(100) 
    ax.plot(Data) 
    test(ax, [.3,.3,.6,.6], Data) 

here is the result: (picture)

EDIT:

私はinset_axesを使用してこの問題を回避できます。

from mpl_toolkits.axes_grid.inset_locator import inset_axes 
def test(ax_main, pos, Data): 
    ax2 = inset_axes(ax_main, weight=pos[2], height=pos[3]) # weight and height are not important here because they will be changed in the next lines 
    ip = InsetPosition(ax_main, pos) 
    ax2.set_axes_locator(ip) 
    pl.hist(Data) 

for i in range(3): 
    ax=pl.subplot(1,3,i+1) 
    Data = pl.rand(100) 
    ax.plot(Data) 
    test(ax, [.3,.3,.6,.6], Data) 

答えて

0

二つの問題がここにあります。最初のものは分かりやすい。 documentation of figure.add_axesと書いてある通り:

数字に既に同じパラメータの軸がある場合、その軸を現在のものにして戻すだけです。この動作はMatplotlib 2.1以降では廃止されました。一方、この動作が望ましくない場合(つまり、新しいAxesの作成を強制したい場合)、argsとkwargsの一意のセットを使用する必要があります。 axes label属性は、この目的のために公開されています。それ以外の点ではFigureに追加する2つの軸を必要とする場合は、一意のラベルを付けるようにしてください。

これは、plt.axes([0, 0, 1, 1])を何度か呼び出すと、結局新しい軸を作成しないことを意味します。解決策は、ラベルを追加することである。関数のデータとともに、ループ変数iを供給し、

plt.axes([0, 0, 1, 1], label=str(i)) 

を呼び出すことにより、残念ながら、これでは十分ではありませんし、期待通り上記まだ動作しません。これは私には分かりません。しかし、試行錯誤によって、軸の初期位置に異なる座標を使用すると、この作業が可能になることがあります。以下、私はax2 = plt.axes([0.0, 0.0, 0.1, 0.1], label=str(i))を使用しています。 [0.2, 0.0, 0.5, 0.1]もうまくいくと思われますが、[0.2, 0.2, 0.5, 0.1]は動作しません。さらに奇妙なことに、[0.0, 0.2, 0.5, 0.1]は2番目と3番目のプロットにはインセットを作成しますが、最初のプロットには挿入しません。

このすべてが少しは任意ですが、解決のための十分なようだ:明確化のため

import matplotlib.pyplot as plt 
from mpl_toolkits.axes_grid1.inset_locator import InsetPosition 

def test(ax_main, pos, Data,i): 
    ax2 = plt.axes([0.0, 0.0, 0.1, 0.1], label=str(i)) 
    ip = InsetPosition(ax_main, pos) 
    ax2.set_axes_locator(ip) 
    ax2.plot(Data) 


for i in range(3): 
    ax=plt.subplot(1,3,i+1) 
    test(ax, [.3,.3,.6,.6], [1,2], i) 

plt.show() 

enter image description here

+0

おかげで、私はまた、リポジトリにこのスレッドを見たが、それ以降をインストールしてみてくださいませんでしたmatplotlibのバージョン:[https://github.com/matplotlib/matplotlib/issues/9024](https://github.com/matplotlib/matplotlib/issues/9024) – ses

関連する問題