Oleg
Oleg

Reputation: 10676

jupyter notebook prints output even when suppressed

Why is it printing the bins from the histogram? Shouldn't the semicolon suppress it?

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

enter image description here

Upvotes: 1

Views: 549

Answers (2)

Matt
Matt

Reputation: 27843

You've set InteractiveShell.ast_node_interactivity = "all";, so you've set all nodes to have ast interactivity enabled. So you get the values of data = {..}

And ; works only for the last top level expression, axes[idx].hist(data[k], bins=20); is not a top level expression, as it is nested in the for, the 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; # or None; 0; "foo"; ... 

And you won't have any outputs.

Use codetransformer %%ast magic to quickly see the ast of an expression.

Upvotes: 2

Ted Petrou
Ted Petrou

Reputation: 61947

If you read the documentation, you will see exactly what it returns - a three item tuple described below. You can display it in the notebook by placing a ? at the end of the call to the histogram. It looks like your InteractiveShell is making it display. Normally, yes a semicolon would suppress the output, although inside of a loop it would be unnecessary.

Returns

n : array or list of arrays The values of the histogram bins. See normed and weights for a description of the possible semantics. If input x is an array, then this is an array of length nbins. If input is a sequence arrays [data1, data2,..], then this is a list of arrays with the values of the histograms for each of the arrays in the same order.

bins : array The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin). Always a single array even when multiple data sets are passed in.

patches : list or list of lists Silent list of individual patches used to create the histogram or list of such list if multiple input datasets.

Upvotes: -1

Related Questions