问题 如何在Ipython中显示内联matplotlib图的打印语句?


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

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

%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中有直接的解决方案,还是解决方案或替代解决方案来获得隔行扫描输出?


11704
2017-07-17 19:09


起源

如果你正在使用 %matplotlib inline,你可以打电话 fig.show() 当你想要绘制情节时,我想。 - Thomas K
编辑包括 %matplotlib inline。另外打电话 fig.show() 后 ax.hist(data) 没有改变结果。 - foghorn
关于什么 plt.show()? - Thomas K
plt.show() 做了伎俩 - foghorn


答案:


有简单的解决方案,在绘图后使用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

此代码将按要求工作


10
2018-03-28 11:07



这应该是默认值! - Brick