如何动画matplotlib阴谋的时间有序 [英] How to animate a time-ordered sequence of matplotlib plots

查看:170
本文介绍了如何动画matplotlib阴谋的时间有序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制的matplotlib png格式的图像序列。我们的目标是画出他们迅速模拟电影的效果,但我有想避免实际创建.avi文件或保存matplotlib数字,然后按顺序查看他们的Python之外的其他原因。

I want to plot a sequence of .png images in matplotlib. The goal is to plot them rapidly to simulate the effect of a movie, but I have additional reasons for wanting to avoid actually creating an .avi file or saving matplotlib figures and then viewing them in sequence outside of Python.

我特别想查看图像文件按顺序在Python for循环中。假设我已经正确导入matplotlib,我有我自己的功能new_image()'和'new_rect()',这里的一些例子code失败,因为该节目的阻挡效应来工作()函数的调用GUI主循环:

I'm specifically trying to view the image files in sequence inside a for-loop in Python. Assuming I have imported matplotlib correctly, and I have my own functions 'new_image()' and 'new_rect()', here's some example code that fails to work because of the blocking effect of the show() function's call to the GUI mainloop:

 for index in index_list:
     img = new_image(index)
     rect = new_rect(index)

     plt.imshow(img)
     plt.gca().add_patch(rect)
     plt.show()

     #I also tried pausing briefly and then closing, but this doesn't
     #get executed due to the GUI mainloop from show()
     time.sleep(0.25)
     plt.close()

以上code工作,只显示第一个图像,但该程序只是挂起,等待我去手动关闭所得数字窗口。一旦我做关闭它,然后程序只是挂起,不重情节与新的图像数据。我应该怎么做?另外请注意,我试图与一个plt.draw()命令替换plt.show()命令,然后加入对 - 环外的plt.show()。这不显示任何东西,只是挂起。

The above code works to show only the first image, but then the program just hangs and waits for me to manually close the resultant figure window. Once I do close it, the program then just hangs and doesn't re-plot with the new image data. What should I be doing? Also note that I have tried replacing the plt.show() command with a plt.draw() command, and then adding the plt.show() outside of the for-loop. This doesn't display anything and just hangs.

推荐答案

我已经找到了,这是用命令的最好方法 pylab.ion()后进口pylab。

The best way I have found for this was with the command pylab.ion() after you import pylab.

下面是一个脚本,它使用显示(),但它显示了不同的地块每次 pylab.draw()被调用,这让剧情窗口显示下去。它使用简单的输入逻辑,以决定何时关闭的数字(因为使用显示()表示pylab将不处理的窗户X按钮点击),但是这应该是简单添加到您的GUI的另一个按钮或文本字段。

Here is a script that does use show(), but which displays the different plots each time pylab.draw() is called, and which leaves the plot windows showing indefinitely. It uses simple input logic to decide when to close the figures (because using show() means pylab won't process clicks on the windows x button), but that should be simple to add to your gui as another button or as a text field.

import numpy as np
import pylab
pylab.ion()

def get_fig(fig_num, some_data, some_labels):

    fig = pylab.figure(fig_num,figsize=(8,8),frameon=False)
    ax = fig.add_subplot(111)
    ax.set_ylim([0.1,0.8]); ax.set_xlim([0.1, 0.8]);
    ax.set_title("Quarterly Stapler Thefts")
    ax.pie(some_data, labels=some_labels, autopct='%1.1f%%', shadow=True);
    return fig

my_labels = ("You", "Me", "Some guy", "Bob")

# To ensure first plot is always made.
do_plot = 1; num_plots = 0;

while do_plot:
    num_plots = num_plots + 1;
    data = np.random.rand(1,4).tolist()[0]

    fig = get_fig(num_plots,data,my_labels)
    fig.canvas.draw()
    pylab.draw()

    print "Close any of the previous plots? If yes, enter its number, otherwise enter 0..."
    close_plot = raw_input()

    if int(close_plot) > 0:
        pylab.close(int(close_plot))

    print "Create another random plot? 1 for yes; 0 for no."
    do_plot = raw_input();

    # Don't allow plots to go over 10.
    if num_plots > 10:
        do_plot = 0

pylab.show()

通过在此处修改的基本逻辑,我可以把它关闭窗口和情节连续的图像来模拟播放电影,或者我可以保持在它通过电影如何几步键盘控制。

By modifying the basic logic here, I can have it close windows and plot images consecutively to simulate playing a movie, or I can maintain keyboard control over how it steps through the movie.

注意:这为我工作跨平台,似乎严格优于上述窗口帆布经理的做法,并不需要'TkAgg'选项

Note: This has worked for me across platforms and seems strictly superior to the window canvas manager approach above, and doesn't require the 'TkAgg' option.

这篇关于如何动画matplotlib阴谋的时间有序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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