简体   繁体   English

使用matplotlib的python 2.7 mac osx交互式绘图不起作用

[英]python 2.7 mac osx interactive plotting with matplotlib not working

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? 我会弹出一个绘图窗口,但是没有数据出现,也没有重绘...知道我在做什么错吗?

The issue you are having is due to the way that gui mainloops work. 您遇到的问题归因于gui mainloops的工作方式。 When ever you plot call draw events get added to the queue of events for the main loop to process. 每当您绘制调用draw事件时,都会将其添加到事件队列中以供主循环处理。 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 添加一个plt.pause(.1)将暂停循环并允许主循环(冒着拟人化的危险)“屏住呼吸”并更新屏幕上的小部件

Related: 有关:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM