Ipython: 绘图时分号不抑制

创建于 2017-09-12  ·  3评论  ·  资料来源: ipython/ipython

问了 SO并在这里重现。 有两个单元格的笔记本:

在 [1]

import numpy as np
import matplotlib.pyplot as plt
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all";

在 [2]

%matplotlib inline
data ={'first':np.random.rand(100), 
       'second':np.random.rand(100)}
fig, axes = plt.subplots(2)
for idx, k in enumerate(data):
    axes[idx].hist(data[k], bins=20);

不抑制plt.hist()的输出:
screen shot 2017-09-12 at 20 43 35

python -c "import IPython; print(IPython.sys_info())"

{'commit_hash': 'd86648c5d',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/okomarov/anaconda/lib/python3.6/site-packages/IPython',
 'ipython_version': '6.1.0',
 'os_name': 'posix',
 'platform': 'Darwin-16.7.0-x86_64-i386-64bit',
 'sys_executable': '/Users/okomarov/anaconda/bin/python3',
 'sys_platform': 'darwin',
 'sys_version': '3.6.2 |Anaconda custom (x86_64)| (default, Jul 20 2017, '
                '13:14:59) \n'
                '[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]'}

最有用的评论

您已设置InteractiveShell.ast_node_interactivity = "all"; ,因此您已将所有节点设置为启用了 ast 交互性。

;仅适用于最后一个顶级表达式, axes[idx].hist(data[k], bins=20);不是顶级,因为它嵌套在for ,最后一个顶级节点是for ,这是一个声明。

只需添加最后一个 no-op 语句,并以;

%matplotlib inline
data ={'first':np.random.rand(100), 
       'second':np.random.rand(100)};
fig, axes = plt.subplots(2);
for idx, k in enumerate(data):
    axes[idx].hist(data[k], bins=20)
pass;

你不会有任何输出。

所有3条评论

您已设置InteractiveShell.ast_node_interactivity = "all"; ,因此您已将所有节点设置为启用了 ast 交互性。

;仅适用于最后一个顶级表达式, axes[idx].hist(data[k], bins=20);不是顶级,因为它嵌套在for ,最后一个顶级节点是for ,这是一个声明。

只需添加最后一个 no-op 语句,并以;

%matplotlib inline
data ={'first':np.random.rand(100), 
       'second':np.random.rand(100)};
fig, axes = plt.subplots(2);
for idx, k in enumerate(data):
    axes[idx].hist(data[k], bins=20)
pass;

你不会有任何输出。

@Carreau感谢您的澄清!

我找不到关于:

和 ; 仅适用于最后一个顶级表达式

那是在某处记录的吗? 您想在 SO 中复制粘贴您的答案,以便我可以接受吗?

那是在某处记录的吗?

可能不是, ast_interactivity选项很少使用,我们并不真正关心区别,因为 _most_ 用例仅用于最后一个表达式。

你想复制粘贴你的答案,所以我可以接受吗?

已经做到了,还有其他一些细节:-)

此页面是否有帮助?
0 / 5 - 0 等级