1

3つのプロットを持つフィギュアキャンバスがあるとします...2つはimshowでプロットされた同じ寸法の画像で、もう1つは他の種類のサブプロットです。imshowプロットのx軸とy軸をリンクして、一方をズームインすると(NavigationToolbarが提供するズームツールを使用)、もう一方が同じ座標にズームし、一方をパンすると、 、他の鍋も同様です。

scatterやhistogramなどのサブプロットメソッドは、sharexとshareyの軸を指定するkwargsを渡すことができますが、imshowにはそのような構成はありません。

NavigationToolbar2WxAgg(以下に表示)をサブクラス化することで、これを回避する方法をハッキングし始めました...しかし、ここにはいくつかの問題があります。1)これは、キャンバス内のすべてのプロットの軸をリンクします。これは、a.in_axes()のチェックを削除するだけなので、2)これはパンにはうまく機能しましたが、ズームにより、すべてのサブプロットが同じグローバルからズームされましたそれぞれの軸の同じポイントからではなく、ポイント。

誰かが回避策を提案できますか?

どうもありがとう!-アダム

from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg
class MyNavToolbar(NavigationToolbar2WxAgg):
    def __init__(self, canvas, cpfig):
        NavigationToolbar2WxAgg.__init__(self, canvas)

    # overrided
    # As mentioned in the code below, the only difference here from overridden
    # method is that this one doesn't check a.in_axes(event) when deciding which
    # axes to start the pan in...
    def press_pan(self, event):
        'the press mouse button in pan/zoom mode callback'

        if event.button == 1:
            self._button_pressed=1
        elif  event.button == 3:
            self._button_pressed=3
        else:
            self._button_pressed=None
            return

        x, y = event.x, event.y

        # push the current view to define home if stack is empty
        if self._views.empty(): self.push_current()

        self._xypress=[]
        for i, a in enumerate(self.canvas.figure.get_axes()):
            # only difference from overridden method is that this one doesn't
            # check a.in_axes(event)
            if x is not None and y is not None and a.get_navigate():
                a.start_pan(x, y, event.button)
                self._xypress.append((a, i))
                self.canvas.mpl_disconnect(self._idDrag)
                self._idDrag=self.canvas.mpl_connect('motion_notify_event', self.drag_pan)

    # overrided
    def press_zoom(self, event):
        'the press mouse button in zoom to rect mode callback'
        if event.button == 1:
            self._button_pressed=1
        elif  event.button == 3:
            self._button_pressed=3
        else:
            self._button_pressed=None
            return

        x, y = event.x, event.y

        # push the current view to define home if stack is empty
        if self._views.empty(): self.push_current()

        self._xypress=[]
        for i, a in enumerate(self.canvas.figure.get_axes()):
            # only difference from overridden method is that this one doesn't
            # check a.in_axes(event) 
            if x is not None and y is not None and a.get_navigate() and a.can_zoom():
                self._xypress.append(( x, y, a, i, a.viewLim.frozen(), a.transData.frozen()))

        self.press(event)
4

1 に答える 1

3

sharexをadd_subplotに渡すことができることに気づいていませんでした...

f = plt.figure()
ax1 = f.add_subplot(211)
ax2 = f.add_subplot(212, sharex=ax1, sharey=ax1)
ax1.imshow(np.random.randn(10,10), interpolation='nearest')
ax2.imshow(np.random.randn(10,10), interpolation='nearest')
f.canvas.draw()

ただし、次の操作を実行して、画像を表示するための軸オブジェクトを設定しようとした場合。

ax1.axis('image')

あなたが得るでしょう:

ValueError: adjustable must be "datalim" for shared axes

matplotlibの人たちは私が使うことができると言っています

ax1.set_adjustable("box-forced")
ax2.set_adjustable("box-forced")

代わりに、matplotlibバージョン0.98.5.3を使用しても機能しません...新しいバージョンでは機能すると思います。返信があり次第更新します。

于 2010-05-27T19:19:51.440 に答える