如何在 Ipython 中显示与内联的 matplotlib 图交错的打印语句?

How to display print statements interlaced with matplotlib plots inline in Ipython?

我希望打印语句的输出与绘图交错,按照它们在 Ipython 笔记本单元格中打印和绘制的顺序。例如,考虑以下代码:

(用 ipython notebook --no-browser --no-mathjax 启动 ipython)

%matplotlib inline
import matplotlib.pyplot as plt

i = 0
for data in manydata:
    fig, ax = plt.subplots()
    print "data number i =", i
    ax.hist(data)
    i = i + 1

理想情况下输出如下:

data number i = 0
(histogram plot)
data number i = 1
(histogram plot)
...

然而,Ipython 中的实际输出将如下所示:

data number i = 0
data number i = 1
...
(histogram plot)
(histogram plot)
...

在 Ipython 中是否有直接解决方案,或者有解决方法或替代解决方案来获得隔行扫描输出?

有简单的解决方法,画图后使用matplotlib.pyplot.show()函数

这将在执行下一行代码之前显示图形

%matplotlib inline
import matplotlib.pyplot as plt

i = 0
for data in manydata:
    fig, ax = plt.subplots()
    print "data number i =", i
    ax.hist(data)
    plt.show() # this will load image to console before executing next line of code
    i = i + 1

此代码将按要求工作