2016-07-22 10 views
2

以下のPython/pyplotコードは、4つのFigureと4つのウィンドウを生成します。私はfig1を表示する1つのウィンドウを開くコードが必要です。ユーザーが右矢印ボタンまたは右矢印キーを押すと、同じウィンドウがfig1をクリアしてfig2を表示します。したがって、基本的に4つの図のうちの1つだけがスライドショーで表示するためにユーザーによって選択されます。私は、ドキュメントやオンラインで成功していない答えを探しました。私は4つの数字に現れる6つの軸の定義を示すために質問を編集しました。 Axesを1つのFigureに関連付ける必要があり、次に、デフォルトのGUIでスライドショーをシミュレートするために軸を描画、消去、再描画する必要がありますか?matplotlibスライドショーを作成するにはどうすればいいですか?

import numpy as np 
import matplotlib.pyplot as plt 

fig1 = plt.figure() 
ax1 = fig1.add_subplot(3, 1, 1) 
ax2 = fig1.add_subplot(3, 1, 2, sharex=ax1) 
ax3 = fig1.add_subplot(3, 1, 3, sharex=ax1) 
fig2 = plt.figure() 
ax4 = fig2.add_subplot(1, 1, 1) 
fig3 = plt.figure() 
ax5 = fig2.add_subplot(1, 1, 1) 
fig4 = plt.figure() 
ax6 = fig2.add_subplot(1, 1, 1) 
plt.show() 

理想的には、MacOS、Linux、およびWindowsで同じコード機能を保証するためにバックエンドを設定したいと考えています。しかし、私はWindows 7で動作する非常に基本的なスライドショーを取得し、必要に応じて後で他のOS用に開発することに満足しています。

答えて

2

たぶん、このような何か: (切り替えるために、グラフをクリックしてください)

import matplotlib.pyplot as plt 
import numpy as np 

i = 0 

def fig1(fig): 
    ax = fig.add_subplot(111) 
    ax.plot(x, np.sin(x)) 


def fig2(fig): 
    ax = fig.add_subplot(111) 
    ax.plot(x, np.cos(x)) 


def fig3(fig): 
    ax = fig.add_subplot(111) 
    ax.plot(x, np.tan(x)) 


def fig4(fig): 
    ax1 = fig.add_subplot(311) 
    ax1.plot(x, np.sin(x)) 
    ax2 = fig.add_subplot(312) 
    ax2.plot(x, np.cos(x)) 
    ax3 = fig.add_subplot(313) 
    ax3.plot(x, np.tan(x)) 

switch_figs = { 
    0: fig1, 
    1: fig2, 
    2: fig3, 
    3: fig4 
} 

def onclick1(fig): 
    global i 
    print(i) 
    fig.clear() 
    i += 1 
    i %= 4 
    switch_figs[i](fig) 
    plt.draw() 

x = np.linspace(0, 2*np.pi, 1000) 
fig = plt.figure() 
switch_figs[0](fig) 
fig.canvas.mpl_connect('button_press_event', lambda event: onclick1(fig)) 

plt.show() 
+0

これがうまく私の問題を解決します!私はこの回答を受け入れましたb/cフルスクリーンとサイズ変更をサポートするWindows 7のデフォルトGUIで動作します。各def文の中でfig.set_tight_layout(True)を使用しても動作します。しかし、100 dpiで10 x 6.5インチの図形サイズを定義すると、フルサイズでFigureを再描画する際に問題があります。私は印刷のサポートのための別のコードを記述します。 – SystemTheory

関連する問題