Python Matplotlib图不';重新打开时无法正确显示

Python Matplotlib图不';重新打开时无法正确显示,python,matplotlib,Python,Matplotlib,我有一个从pythonshell运行的应用程序。我有一个主要人物 fig, (plt1, plt2, plt3) = pl.subplots(3, 1) 我有以下职能: def do_select_features(self, line): global mode, fig mode = 'features' fig.canvas.mpl_connect('key_press_event', on_key) plot_wnd() pl.ion()

我有一个从pythonshell运行的应用程序。我有一个主要人物

fig, (plt1, plt2, plt3) = pl.subplots(3, 1)
我有以下职能:

def do_select_features(self, line):
    global mode, fig
    mode = 'features'
    fig.canvas.mpl_connect('key_press_event', on_key)
    plot_wnd()
    pl.ion()
    pl.show()

def plot_wnd():
    plt1.cla()
    plt2.cla()
    plt3.cla()
    plt1.grid(True)
    plt2.grid(True)
    plt3.grid(True)
    plt1.hold(True)
    plt1.plot(rdata)
    plt1.plot([rng_current[0], rng_current[0]], [min(rdata), max(rdata)], 'r', lw=1)
    plt1.plot([rng_current[1], rng_current[1]], [min(rdata), max(rdata)], 'r', lw=1)
    zdata = compression.fill_in(compression.zigzag(normalize(rdata), zigzag))
    plt2.plot(zdata[rng_current[0]:rng_current[1]], color='r')
    plt2.plot(normalize(rdata)[rng_current[0]:rng_current[1]], color='b')
    plt3.plot(diff(zdata, normalize(rdata))[rng_current[0]:rng_current[1]], color='r')
    pl.draw()

def on_key(event):
    global rng_current

    if event.key == 'right' and (rng_current[1] + stp) < len(rdata):
        rng_current[0] += stp
        rng_current[1] += stp
        plot_wnd()

    if event.key == 'enter':
        f = h5py.File(db, "a")
        pdo.insert_group(f, mode, compression.zigzag(normalize(rdata[rng_current[0]:rng_current[1]]), zigzag))
        f.close
        print "Subseries added to ", mode, " database"

    if event.key == 'left' and (rng_current[0] - stp) > 0:
        rng_current[0] -= stp
        rng_current[1] -= + stp
        plot_wnd()

    if event.key == 'escape':
        pl.close()
def do_select_功能(自身,行):
全局模式,图
模式='features'
图canvas.mpl\u connect('key\u press\u event',on\u key)
图_wnd()
pl.ion()
pl.show()
def plot_wnd():
plt1.cla()
plt2.cla()
plt3.cla()
plt1.grid(真)
plt2.网格(真)
plt3.网格(真实)
plt1.hold(正确)
plt1.绘图(rdata)
plt1.绘图([rng_电流[0],rng_电流[0],[min(rdata),max(rdata)],'r',lw=1)
plt1.绘图([rng_电流[1],rng_电流[1],[min(rdata),max(rdata)],'r',lw=1)
zdata=压缩。填充(压缩。锯齿形(标准化(rdata),锯齿形))
plt2.绘图(zdata[rng\u current[0]:rng\u current[1]],color='r')
plt2.绘图(标准化(rdata)[rng_电流[0]:rng_电流[1]],颜色为'b')
plt3.plot(diff(zdata,normalize(rdata))[rng_current[0]:rng_current[1]],color='r')
pl.draw()
def on_键(事件):
全球rng_电流
如果event.key==‘right’和(rng_current[1]+stp)0:
rng_电流[0]=stp
rng_电流[1]-=+stp
图_wnd()
如果event.key=='escape':
pl.关闭()

因此,当我在命令行中时,我调用select_features,它会调出图形,一切都很好。然后,我用鼠标手动退出窗口或按ESC键(这会触发pl.close()。然后,我希望能够再次调用select_功能,但它始终会显示一个类似的灰色窗口。如何解决此问题?

请尝试将您的
fig
创建移动到函数中。关闭窗口时,您会破坏该图形,因此以后对该函数的调用将无法再访问该图形。您需要重新创建一个新的f每次创建新窗口时,我都会查看。

谢谢这项工作……您知道我如何将(plt1、plt2、plt3)作为参数传递到图.canvas.mpl\u connect中的on\u key(“key\u press\u event”,on\u key”),这将减少大量冗余吗