Python:matplotlib-循环,清除并显示同一图上的不同图 [英] Python: matplotlib - loop, clear and show different plots over the same figure

查看:715
本文介绍了Python:matplotlib-循环,清除并显示同一图上的不同图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看一个图如何通过循环使用不同的值而变化.我想在同一情节上看到它.但是我不想保留图中的前一个图.在MATLAB中,可以通过创建图形并在同一图形上绘图来实现.循环结束时将其关闭.

I want to see how a plot varies with different values using a loop. I want to see it on the same plot. But i do not want to remains of the previous plot in the figure. In MATLAB this is possible by creating a figure and just plotting over the same figure. Closing it when the loop ends.

就像

fh = figure();
%for loop here
%do something with x and y    
subplot(211), plot(x);
subplot(212), plot(y); 
pause(1)
%loop done
close(fh);

我在matplotlib中找不到与之等效的东西.通常,所有问题都与在同一图上绘制不同系列有关,这似乎是在matplotlib上自然产生的,方法是使用plt.plot()绘制多个序列,然后最终使用plt.show()将它们全部显示出来.但是我想刷新剧情.

I am not able to find the equivalent of this in matplotlib. Usually all the questions are related to plotting different series on the same plot, which seems to come naturally on matplotlib, by plotting several series using plt.plot() and then showing them all finally using plt.show(). But I want to refresh the plot.

推荐答案

在matplotlib中创建动画本质上有两种不同的方法

There are essentially two different ways to create animations in matplotlib

使用plt.ion()可以打开更多交互式功能.即使尚未调用show,这也将创建一个绘图.可以通过调用plt.draw()或通过动画plt.pause()来更新图.

Turning on interactive more is done using plt.ion(). This will create a plot even though show has not yet been called. The plot can be updated by calling plt.draw() or for an animation, plt.pause().

import matplotlib.pyplot as plt

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,17)
ax1.set_ylim(-400,3000)
plt.ion()

for i in range(15):
    x.append(x[-1]+x[-2])
    line1.set_data(range(len(x)), x)
    y.append(y[-1]+y[-2])
    line2.set_data(range(len(y)), y)

    plt.pause(0.1)

plt.ioff()    
plt.show()

FuncAnimation

Matplotlib提供了动画子模块,它可以简化动画的创建过程,并可以轻松地保存它们.与上面相同,使用FuncAnimation看起来像:

FuncAnimation

Matplotlib provides an animation submodule, which simplifies creating animations and also allows to easily save them. The same as above, using FuncAnimation would look like:

import matplotlib.pyplot as plt
import matplotlib.animation

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,18)
ax1.set_ylim(-400,3000)


def update(i):
    x.append(x[-1]+x[-2])
    line1.set_data(range(len(x)), x)
    y.append(y[-1]+y[-2])
    line2.set_data(range(len(y)), y)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=14, repeat=False)   
plt.show()

一个使频率和功率谱变化的正弦波动画的示例如下:

An example to animate a sine wave with changing frequency and its power spectrum would be the following:

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

x = np.linspace(0,24*np.pi,512)
y = np.sin(x)

def fft(x):
    fft = np.abs(np.fft.rfft(x))
    return fft**2/(fft**2).max()

fig, (ax1,ax2) = plt.subplots(nrows=2)
line1, = ax1.plot(x,y)
line2, = ax2.plot(fft(y))
ax2.set_xlim(0,50)
ax2.set_ylim(0,1)

def update(i):
    y = np.sin((i+1)/30.*x)
    line1.set_data(x,y)
    y2 = fft(y)
    line2.set_data(range(len(y2)), y2)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=60, repeat=True)  
plt.show()

这篇关于Python:matplotlib-循环,清除并显示同一图上的不同图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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