关闭Pyplot窗口 [英] Closing pyplot windows

查看:307
本文介绍了关闭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()是一个 blocking 函数,因此在您上面使用的示例代码中,直到关闭窗口后才执行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,第​​三行指定一个当滑块值时要调用的函数更改(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 属性.

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天全站免登陆