Ipython: semicolon does not suppress when plotting

Created on 12 Sep 2017  路  3Comments  路  Source: ipython/ipython

I asked on SO and reproduce here. A notebook with two cells:

In [1]

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

In [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);

does not suppress the output of the plt.hist():
screen shot 2017-09-12 at 20 43 35

Output from 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)]'}

Most helpful comment

You've set InteractiveShell.ast_node_interactivity = "all";, so you've set all nodes to have ast interactivity enabled.

And ; works only for the last top level expression, axes[idx].hist(data[k], bins=20); is not a top level, as it is nested in the for, the for last top level node is the for, which is a statement.

Simply add a last no-op statement, and end it with ;

%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;

And you wont' have any outputs.

All 3 comments

You've set InteractiveShell.ast_node_interactivity = "all";, so you've set all nodes to have ast interactivity enabled.

And ; works only for the last top level expression, axes[idx].hist(data[k], bins=20); is not a top level, as it is nested in the for, the for last top level node is the for, which is a statement.

Simply add a last no-op statement, and end it with ;

%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;

And you wont' have any outputs.

@Carreau Thanks for the clarification!

I could not find about:

And ; works only for the last top level expression

Is that documented somewhere? Would you like to copy-paste your answer in SO, so I can accept it?

Is that documented somewhere?

Probably not, the ast_interactivity option is so rarely used that we don't really bother with the distinction as _most_ use case are only for the last expression.

Would you like to copy-paste your answer in so, so I can accept it?

Already did, with a couple of other details :-)

Was this page helpful?
0 / 5 - 0 ratings