【问题标题】:matplotlib inline plots all figures after all textmatplotlib inline 在所有文本之后绘制所有图形
【发布时间】:2016-11-02 01:54:37
【问题描述】:

在 jupyter notebook 中,启用%matplotlib inline 后,打印文本和绘制图形的单元格的输出将在显示图形之前显示整个文本。即使在打印文本之前生成了图形(并调用了 show()),也会发生这种情况。

例如:

fig = plt.figure()
fig.add_subplot(111)
fig.show()
print "hello"

将在空图之前显示“你好”。

如何解决这个问题,让每个图都真正内联?

【问题讨论】:

    标签: ipython jupyter jupyter-notebook


    【解决方案1】:

    我想你想明确地display这个数字,使用IPython的显示功能:

    from IPython.display import display
    
    fig = plt.figure()
    fig.add_subplot(111)
    display(fig)
    print("hello")
    

    【讨论】:

    • 这行得通,但需要注意的是,您必须在最后添加 plt.close() 以使其不会再次打印该图。谢谢!
    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    相关资源
    最近更新 更多