关闭 pyplot 窗口 [英] Closing pyplot windows

查看:31
本文介绍了关闭 pyplot 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终

关于关闭 pyplot 窗口的主题,我发现它真的可能不应该使用 pyplot 来完成.SRK 给出了一个很好的例子,说明如何处理将在下面的答案中更新的图.此外,我还偶然发现了如何将 pyplot 图放入 Tkinter 窗口中,并且 Tkinter 比 pyplot 更擅长打开和关闭窗口.这里是如何将pyplot图放入Tk窗口这是一个很好的例子.

What I found on the subject of closing pyplot windows is that it really probably shouldn't be done using pyplot. SRK gives a great example on how to handle plots that will be updated in his answer below. Also I have stumbled across how to put pyplot plots into a Tkinter window, and Tkinter is much more adept at opening and closing windows than pyplot. Here is how to put a pyplot plot into a Tk window also this is a good example.

/最终编辑

我希望能够显示多个图,然后能够从某些代码输入中单独关闭(从屏幕上删除)它们,但我不知道执行此操作的代码输入.

I would like to be able to display several plots and then be able to close (remove from screen) them individually from some code input, but I don't know the code input to do this.

以下是我迄今为止尝试过的.我玩过 show 和 close 命令的位置,但我从中得到的唯一真正结果是没有出现一个或另一个情节,但我无法从屏幕上删除情节.我一直在插入一个 raw_input() 来创建暂停.

Below is what I have tried so far. I have played around with the position of the show and close commands, but the only real result I have gotten from this is to have one or the other plot not come up, but I have not been able to remove a plot from the screen. I have been inserting a raw_input() to create pauses.

这些图是从 Tkinter gui 调用的,如果有更好的方法从那个方向做到这一点,我会很高兴听到它.

These plots are being called from a Tkinter gui and if there is a better way to do this from that direction I would be glad to hear it.

感谢您提供任何意见.

import matplotlib.pyplot as plt

a = range(0,10)
b = range(0,20,2)
c = range(0,30,3)
d = range(0,40,4)

plot1 = plt.figure()
plt.plot(a,b, 'r-o')

plt.show()

plt.close()

plot2 = plt.figure()
plt.plot(c,d, 'b-o')

plt.show()
plt.close() 

编辑代码:这也不起作用.

Edit Code: This didn't work either.

plot1 = plt.figure(1)
plt.plot(a,b, 'r-o')

plot2 = plt.figure(2)
plt.plot(c,d, 'b-o')
#plt.close(1)
#this will prevent plot1 from being displayed
plt.show()
plt.close(1)  # or ('all') or (plot1)

推荐答案

plt.close() 将关闭当前实例.

plt.close(2) 将关闭图 2

plt.close(plot1) 将关闭图与实例 plot1

plt.close(plot1) will close figure with instance plot1

plt.close('all') 将关闭所有文件

此处找到.

记住 plt.show() 是一个 阻塞 函数,所以在你上面使用的示例代码中,plt.close()直到窗口关闭才被执行,这使得它变得多余.

Remember that plt.show() is a blocking function, so in the example code you used above, plt.close() isn't being executed until the window is closed, which makes it redundant.

您可以在代码的开头使用 plt.ion() 使其非阻塞,尽管这还有其他含义.

You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications.

经过我们在评论中的讨论,我整理了一些示例来演示如何使用绘图功能.

After our discussion in the comments, I've put together a bit of an example just to demonstrate how the plot functionality can be used.

下面我创建了一个情节:

Below I create a plot:

fig = plt.figure(figsize=plt.figaspect(0.75))
ax = fig.add_subplot(1, 1, 1)
....
par_plot, = plot(x_data,y_data, lw=2, color='red')

在这种情况下,上面的 ax 是一对轴的句柄.每当我想对这些轴执行某些操作时,我都可以通过调用 axes(ax) 将当前的一组轴更改为该特定组.

In this case, ax above is a handle to a pair of axes. Whenever I want to do something to these axes, I can change my current set of axes to this particular set by calling axes(ax).

par_plot 是 line2D 实例的句柄.这被称为艺术家.如果我想改变行的一个属性,比如改变 ydata,我可以通过引用这个句柄来实现.

par_plot is a handle to the line2D instance. This is called an artist. If I want to change a property of the line, like change the ydata, I can do so by referring to this handle.

我还可以通过执行以下操作来创建滑块小部件:

I can also create a slider widget by doing the following:

axsliderA = axes([0.12, 0.85, 0.16, 0.075])
sA = Slider(axsliderA, 'A', -1, 1.0, valinit=0.5)
sA.on_changed(update)

第一行为滑块创建一个新轴(称为 axsliderA),第二行创建一个滑块实例 sA 放置在轴中,第三行line 指定滑块值更改时要调用的函数(update).

The first line creates a new axes for the slider (called axsliderA), the second line creates a slider instance sA which is placed in the axes, and the third line specifies a function to call when the slider value changes (update).

我的更新函数可能如下所示:

My update function could look something like this:

def update(val):
    A = sA.val
    B = sB.val
    C = sC.val
    y_data = A*x_data*x_data + B*x_data + C
    par_plot.set_ydata(y_data)
    draw()

par_plot.set_ydata(y_data) 使用句柄 par_plot 更改 Line2D 对象的 ydata property.

The par_plot.set_ydata(y_data) changes the ydata property of the Line2D object with the handle par_plot.

draw() 函数更新当前轴集.

综合起来:

from pylab import *
import matplotlib.pyplot as plt
import numpy

def update(val):
    A = sA.val
    B = sB.val
    C = sC.val
    y_data = A*x_data*x_data + B*x_data + C
    par_plot.set_ydata(y_data)
    draw()


x_data = numpy.arange(-100,100,0.1);

fig = plt.figure(figsize=plt.figaspect(0.75))
ax = fig.add_subplot(1, 1, 1)
subplots_adjust(top=0.8)

ax.set_xlim(-100, 100);
ax.set_ylim(-100, 100);
ax.set_xlabel('X')
ax.set_ylabel('Y')

axsliderA = axes([0.12, 0.85, 0.16, 0.075])
sA = Slider(axsliderA, 'A', -1, 1.0, valinit=0.5)
sA.on_changed(update)

axsliderB = axes([0.43, 0.85, 0.16, 0.075])
sB = Slider(axsliderB, 'B', -30, 30.0, valinit=2)
sB.on_changed(update)

axsliderC = axes([0.74, 0.85, 0.16, 0.075])
sC = Slider(axsliderC, 'C', -30, 30.0, valinit=1)
sC.on_changed(update)

axes(ax)
A = 1;
B = 2;
C = 1;
y_data = A*x_data*x_data + B*x_data + C;

par_plot, = plot(x_data,y_data, lw=2, color='red')

show()

关于上述内容的说明:当我运行应用程序时,代码按顺序运行(我认为它将 update 函数存储在内存中),直到它遇到 show(),即阻塞.当您对其中一个滑块进行更改时,它会从内存中运行更新功能(我认为?).

A note about the above: When I run the application, the code runs sequentially right through (it stores the update function in memory, I think), until it hits show(), which is blocking. When you make a change to one of the sliders, it runs the update function from memory (I think?).

这就是 show() 以现在的方式实现的原因,以便您可以通过使用函数来处理数据在后台更改值.

This is the reason why show() is implemented in the way it is, so that you can change values in the background by using functions to process the data.

这篇关于关闭 pyplot 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆