python - 无法调整 matplotlib 窗口大小

标签 python matplotlib

我有一个使用 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/

相关文章:

python - 合并两个 csv 文件,为所有匹配的记录添加一个带有标志值的列

python - bert-serving-start 给出错误 TypeError : cannot unpack non-iterable NoneType object - tried multiple paths to the model

python - 南方迁移和 django 迁移有什么区别?

python - 从 txt 文件绘制简单图形 python

python - 在python matplotlib中连接具有相同值的点

python - 背景中的matplotlib垂直空间

python - django-getlist()

python - 没有名为 'nltk' 的模块

python - 绘图 : vertical gradient fill under curve?

python - Matplotlib 在 Ipython 中不显示交互式图表