【问题标题】:IPython display assignment resultIPython 显示赋值结果
【发布时间】:2017-10-08 20:14:54
【问题描述】:

herehere 已经提出过这个问题。

我很好奇如何复制 Matplotlib 网站上 tutorial 的以下行:

也就是说,如果我在哪里接受上面链接的问题的答案的解释,这应该是不可能的。 两个答案都表明显示分配结果不是 IPython 核心功能的一部分。第二个答案建议使用displaytools 扩展名的解决方法,这需要您附加要由## 显示的分配行。

【问题讨论】:

    标签: python matplotlib ipython jupyter-notebook assign


    【解决方案1】:

    我认为您不能从字面上理解本教程。实际上,这看起来像

    In [5]: img=mpimg.imread('stinkbug.png')
    
    In [6]: img
    Out[6]: 
    array([[[ 0.40784314,  0.40784314,  0.40784314],
            [ 0.40784314,  0.40784314,  0.40784314],
            [ 0.40784314,  0.40784314,  0.40784314],
    

    在 IPython 中。它在教程中看起来不同的原因是它是从源代码自动格式化的。 source of this part 看起来像

    # It's a 24-bit RGB PNG image (8 bits for each of R, G, B).  Depending
    # on where you get your data, the other kinds of image that you'll most
    # likely encounter are RGBA images, which allow for transparency, or
    # single-channel grayscale (luminosity) images.  You can right click on
    # it and choose "Save image as" to download it to your computer for the
    # rest of this tutorial.
    #
    # And here we go...
    
    img = mpimg.imread('../../doc/_static/stinkbug.png')
    print(img)
    
    ###############################################################################
    # Note the dtype there - float32.  Matplotlib has rescaled the 8 bit
    # data from each channel to floating point data between 0.0 and 1.0.
    

    print 语句以某种方式转换为Out[5]

    【讨论】:

    • 是的,显然,调用对象会输出它。如果他们将这种不存在的行为硬编码到教程中,我会发现它非常具有误导性。我的意思是,输出是指令的结果;在这种情况下调用某个对象。为什么作者可能想要制造一个不可重现的输出语法? Out[#outputnumber] 语法确实有意义;对指令In[#instruction-number]的回应。
    • 其实我找到了原因。请参阅更新的答案。在任何情况下,如果您真的认为问题令人困惑,您可以随时通过GitHub issue tracker 提出问题。 (我已经读过这个教程几次了,我从来没有想过这实际上并没有像那里所说的那样工作。)
    • 其实你可以使用 --TerminalInteractiveShell.ast_node_interactivity=... 到alllast_expr_or_assign 来获得显示的行为;虽然这是一个高级选项,只有在你知道自己在做什么的情况下才应该使用。
    • @Matt Case 仍然是他们在教程中“硬编码”了该行,这在教学上并不合理。我提出了一个拉取请求,通过调用 img 直接调用对象来替换 print(img) 行。
    • 是的,我只是指出你在技术上可以,但是我同意从教学上说它不是最好的。感谢您在 MPL 上的 PR。
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多