28

Is it possible to suppress the array output when plotting a histogram in IPython?

For example:

plt.hist(OIR['Range'], bins, named=True, histtype='bar')

outputs/prints the array information before displaying the graph.

IPython histogram

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aozkan
  • 869
  • 2
  • 11
  • 17

3 Answers3

56

Just put ; after the code.

It works only in Jupyter Notebook.

plt.hist(...);

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
weiz
  • 2,935
  • 1
  • 12
  • 5
  • 9
    Brilliant solution! The reason this works is because the notebook shows the return value of the last command. By adding `;` the last command is "nothing" so there is no return value to show. – kynan Nov 06 '15 at 13:26
35

Assign the return value to a variable (which I call _ to indicate it's unused):

_ = plt.hist(...)
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 16
    alternately, you can suppress output by putting a semicolon at the end of the last line: `plot(foo);` – minrk Jan 24 '13 at 19:43
3

You can also add plt.show():

plt.hist(OIR['Range'], bins, named=True, histtype='bar')
plt.show() 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
mendel
  • 327
  • 2
  • 3
  • How does that suppress the output (*"prints the array information before displaying the graph"*)? How does it work? It seems like a ***bogus answer***. Is it? – Peter Mortensen Nov 22 '22 at 20:42
  • The OP has left the building: *"Last seen more than 2 years ago"*. Perhaps somebody else can chime in? – Peter Mortensen Nov 22 '22 at 20:43