gpt4 book ai didi

python - 在 Python Matplotlib 的 while 循环中生成的数字 "not responding"

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:59 32 4
gpt4 key购买 nike

是否可以在一个循环中生成并显示多个图形?我想显示每个图形,以便在继续下一个图形之前对其进行检查。为了让这个问题更清楚,我写了一个我想做的小例子。目前,数字显示为“无响应”,因此我看不到其中的内容。我试过使用 plt.waitforbuttonpress 和 plt.ion,但这似乎不起作用。

我使用的是 spyder 编辑器,但如果该解决方案也可以在其他编辑器中工作(比如空闲),那就太好了。

import numpy as np
import matplotlib.pyplot as plt

pi = 3.14
figureTime = 1.0

x = np.arange(0,2*pi,0.1)
y1 = np.sin(x)
y2 = np.cos(x)

plt.ion()

for i in xrange(1,11,1):

plt.close('all')

# Figure 1
plt.figure()
plt.plot(x,y1*i)
plt.show()
plt.draw()
plt.waitforbuttonpress(timeout=figureTime)
raw_input("Press Enter to continue")
plt.close()


# Figure 2
plt.figure()
plt.plot(x,y2*i)
plt.show()
plt.draw()
plt.waitforbuttonpress(timeout=figureTime)
raw_input("Press Enter to continue")
plt.close()

最佳答案

好的解决了。以下代码适用于 spyder 和 idle。您所要做的就是在循环外调用 plt.ion() ,并在您想暂停图形时调用 plt.waitforbuttonpress() !简单!

import numpy as np
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")

pi = 3.14

x = np.arange(0,2*pi,0.1)
y1 = np.sin(x)
y2 = np.cos(x)

plt.ion()

for i in xrange(1,11,1):

plt.close('all')

# Figure 1
plt.figure()
plt.plot(x,y1*i)
plt.show()
plt.waitforbuttonpress()

# Figure 2
plt.figure()
plt.plot(x,y2*i)
plt.show()
plt.waitforbuttonpress()

关于python - 在 Python Matplotlib 的 while 循环中生成的数字 "not responding",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28567717/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com