python 2.7 mac osx interactive plotting with matplotlib not working

家住魔仙堡 提交于 2020-01-16 00:50:43

问题


From here I found this code:

import random
from matplotlib import pyplot as plt
import numpy as np

plt.ion() # interactive mode
ydata = [0] * 50 

# make plot
ax1 = plt.axes() 
line, = plt.plot(ydata)
plt.ylim([0, 100]) # set the y-range

while True:
    randint = int(random.random() * 100)
    ymin = float(min(ydata)) - 10
    ymax = float(max(ydata)) + 10
    plt.ylim([ymin,ymax])
    ydata.append(randint)
    del ydata[0]
    line.set_xdata(np.arange(len(ydata)))
    line.set_ydata(ydata)  # update data
    plt.draw() # update plot

I get a plot window that pops up, but no data appears and nothing gets redrawn...any idea what I'm doing wrong?


回答1:


The issue you are having is due to the way that gui mainloops work. When ever you plot call draw events get added to the queue of events for the main loop to process. If you add them as fast as possible the loop can never clear it's queue and actually draw to the screen.

Adding a plt.pause(.1) will pause the loop and allow the main loop (at the risk of being anthropomorphic) 'catch it's breath' and update the widgets on the screen

Related:

  • Python- 1 second plots continous presentation
  • matplotlib qt imshow animate
  • Figure GUI freezing
  • make matplotlib draw() only show new point


来源:https://stackoverflow.com/questions/19105388/python-2-7-mac-osx-interactive-plotting-with-matplotlib-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!