【问题标题】:Figures "not responding" when generated in a while loop in Python Matplotlib在 Python Matplotlib 的 while 循环中生成的数字“没有响应”
【发布时间】:2015-02-17 17:46:27
【问题描述】:

是否可以在一个循环中生成并显示多个图形?我想显示每个图形,以便在继续下一个之前检查它。为了让这个问题更清楚,我写了一个我想做的小例子。目前,这些数字显示为“无响应”,所以我看不到其中的内容。我尝试过使用 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()

【问题讨论】:

    标签: python loops matplotlib figure


    【解决方案1】:

    好的解决了。以下代码适用于 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()
    

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      相关资源
      最近更新 更多