本文介绍了使用matplotlib的python 2.7 mac osx交互式绘图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处中,我找到了以下代码:

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?

推荐答案

您遇到的问题归因于gui mainloop的工作方式.每当您绘制调用draw时,事件都会添加到事件队列中,以供主循环处理.如果您尽可能快地添加它们,则循环永远无法清除它的队列,而实际上无法绘制到屏幕上.

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.

添加plt.pause(.1)将暂停循环并允许主循环(冒着拟人化的危险)屏住呼吸"并更新屏幕上的小部件

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

相关:

这篇关于使用matplotlib的python 2.7 mac osx交互式绘图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!