如何让 python matplotlib 刷新所有图形

How to get python matplotlib to flush all graphics

我正在研究一些大型数据集,这些数据集会随着我可以控制的某些参数而变化。 我现在想在同一图中的子图中绘制我的数据分布。但是,由于数据非常大,构建直方图需要一些时间。

所以,我希望在完成时绘制子图,如下所示。

import matplotlib.pyplot as plt
import numpy as np
import numpy.random as npr

Ne=10
MC=1000000
f1d,f2d = plt.figure(),plt.figure()
for Part in range(Ne):
    datax=npr.normal(size=MC)+4*Part/Ne ##Simulates my big data
    datay=npr.normal(size=MC) ##Simulates my big data
    ###The 1d histogram
    sf1d = f1d.add_subplot(1,Ne,Part+1,)    
    sf1d.hist(datax,bins=20,normed=True,histtype='stepfilled',alpha=0.5)
    sf1d.hist(datay,bins=20,normed=True,histtype='stepfilled',alpha=0.5)
    plt.show(block=False)

    ###Some flush argument()

    ###The 2d histogram
    sf2d = f2d.add_subplot(1,Ne,Part+1)
    sf2d.hist2d(datax,datay,bins=20,normed=True)
    plt.show(block=False)

    ###Some flush argument()

但是,python 不会实时绘制所有子图,而是会缓冲这些图直到循环完成。 如何强制 matplotlib 立即刷新子图?

来自matplotlib docs

matplotlib.pyplot.draw()

Redraw the current figure.

This is used in interactive mode to update a figure that has been altered, but not automatically re-drawn.

A more object-oriented alternative, given any Figure instance, fig, that was created using a pyplot function, is:

fig.canvas.draw_idle()