我有一个使用 matplotlib 的绘图,它每秒更新一次。它仅用于缓慢监控,因此我遵循了一次又一次清除和绘制的简单方法,这不是最佳选择,但我想要简单性。
my_fig = plt.figure()
ax1 = plt.subplot(111)
plt.show(block=False)
while True:
data = read_data_and_process(...)
ax1.plot_date(data[0], data[1], '-')
my_fig.autofmt_xdate()
plt.draw()
time.sleep(1)
ax1.cla()
它有效,但如果我调整窗口大小,绘图不会改变其大小。如果我在不更新的情况下绘制数据,我可以调整窗口大小,并且绘图也会相应调整大小:
my_fig = plt.figure()
ax1 = plt.subplot(111)
data = read_data_and_process(...)
ax1.plot_date(data[0], data[1], '-')
my_fig.autofmt_xdate()
plt.show(block=True)
如何才能在更新数据时调整第一个示例中的窗口大小?
谢谢!
最佳答案
当我尝试这段代码时,我什至无法捕获窗口来调整它的大小,因为 matplotlib 后端的事件循环被卡住了。我建议查看Animation API对于 matplotlib(请参阅 examples )。
但是,这里有一个快速技巧,可以通过强制 Qt 后端使您的示例正常工作。
import matplotlib
matplotlib.use('QT4Agg')
from matplotlib import pyplot as plt
from PyQt4 import QtCore,QtGui
import time
# The rest of your code as normal
# then at the bottom of the while loop
plt.draw()
QtGui.qApp.processEvents()
time.sleep(1)
ax1.cla()
底线是这不应该在“生产”代码中使用,但作为一种快速破解,它肯定可以工作。
关于python - 无法调整 matplotlib 窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21263283/