如何为按时间顺序排列的 matplotlib 图设置动画 [英] How to animate a time-ordered sequence of matplotlib plots

查看:49
本文介绍了如何为按时间顺序排列的 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()",下面是一些示例代码,由于 show() 函数调用 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()

上面的代码只显示第一个图像,但程序只是挂起并等待我手动关闭生成的图形窗口.一旦我关闭它,程序就会挂起并且不会使用新的图像数据重新绘制.我该怎么办?另请注意,我尝试用 plt.draw() 命令替换 plt.show() 命令,然后在 for 循环之外添加 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 后使用命令 pylab.ion().

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

这是一个使用 show() 的脚本,但每次调用 pylab.draw() 时都会显示不同的图,并离开图窗口无限期地显示.它使用简单的输入逻辑来决定何时关闭图形(因为使用 show() 意味着 pylab 不会处理 windows 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天全站免登陆