我可以在每个循环中使用Matplotlib生成并显示不同的图像吗? [英] Can I generate and show a different image during each loop with Matplotlib?

查看:429
本文介绍了我可以在每个循环中使用Matplotlib生成并显示不同的图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Matplotlib和Python的新手.我主要使用Matlab.目前,我正在使用要运行循环的Python代码.在每个循环中,我将进行一些数据处理,然后根据处理后的数据显示图像.当我进入下一个循环时,我希望关闭先前存储的图像并根据最新数据生成一个新图像.

I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am working with a Python code where I want to run a loop. In each loop, I will do some data processing and then show an image based on the processed data. When I go to the next loop, I want the previously stored image to be closed and generate a new image based on the latest data.

换句话说,我想要一个与以下Matlab代码等效的python代码:

In other words, I want a python code equivalent to the following Matlab code:

x = [1 2 3];

for loop = 1:3

    close all;

    y = loop * x;

    figure(1);

    plot(x,y)

    pause(2)

end

我尝试了以下python代码来实现我的目标:

I tried the following python code to achieve my goal:

import numpy as np
import matplotlib
import matplotlib.lib as plt

from array import array
from time import sleep

if __name__ == '__main__':

    x = [1, 2, 3]

    for loop in range(0,3):

        y = numpy.dot(x,loop)

        plt.plot(x,y)

       plt.waitforbuttonpress

    plt.show()

此代码将所有图叠加在同一图中.如果将plt.show()命令放入for循环内,则仅显示第一张图像.因此,我无法在Python中复制Matlab代码.

This code puts all plots superimposed in the same figure. If I put the plt.show() command inside the for loop, only the first image is shown. Therefore, I could not replicate my Matlab code in Python.

推荐答案

尝试一下:

import numpy
from matplotlib import pyplot as plt

if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion() # turn on interactive mode
    for loop in range(0,3):
        y = numpy.dot(x, loop)
        plt.figure()
        plt.plot(x,y)
        plt.show()
        _ = input("Press [enter] to continue.")

如果要关闭前一个图,则在显示下一个图之前:

if you want to close the previous plot, before showing the next one:

import numpy
from matplotlib import pyplot as plt
if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion() # turn on interactive mode, non-blocking `show`
    for loop in range(0,3):
        y = numpy.dot(x, loop)
        plt.figure()   # create a new figure
        plt.plot(x,y)  # plot the figure
        plt.show()     # show the figure, non-blocking
        _ = input("Press [enter] to continue.") # wait for input from the user
        plt.close()    # close the figure to show the next one.

plt.ion()启用交互模式,使plt.show处于非阻塞状态.

plt.ion() turns on interactive mode making plt.show non-blocking.

这是您的matlab代码的副本:

and heres is a duplicate of your matlab code:

import numpy
import time
from matplotlib import pyplot as plt

if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion()
    for loop in xrange(1, 4):
        y = numpy.dot(loop, x)
        plt.close()
        plt.figure()
        plt.plot(x,y)
        plt.draw()
        time.sleep(2)

这篇关于我可以在每个循环中使用Matplotlib生成并显示不同的图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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