3

This is how I am plotting

from matplotlib import pyplot
pyplot.figure();
pyplot.scatter(x=data[feat], y=data[target]);
pyplot.xlabel(feat);
pyplot.ylabel(target);
pyplot.show();

And I get output like

Figure size 432x288 with 0 Axes>

<matplotlib.collections.PathCollection at 0x7fd80c2fbf50>

Text(0.5,0,'Age1')

Text(0,0.5,'Target')

How can I suppress this output? The semicolon did not work. I am running this in a jupyter notebook.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Baron Yugovich
  • 3,843
  • 12
  • 48
  • 76

2 Answers2

6

The issue is documented here https://github.com/ipython/ipython/issues/10794

You/Something has probably set your interactive shell to from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"

One solution is the change all to last_expr

The consequence of this is that a cell will display the output of the last expression rather than every expression. You could still of course print something and that would work normally.

Another solution is to add ; to the last expression

for example, you could add pass;at the end of the cells, which would suppress output

A third option is as @sacul suggested, to assign outputs to variables by using some dummy variable.

Make sure you're not using that variable elsewhere

hsnee
  • 543
  • 2
  • 6
  • 17
4

Assign your calls to plot to a random variable name, and there won't be any output. By convention, this could be _, but you can use whatever variable name you want:

from matplotlib import pyplot

_ = pyplot.figure()
_ = pyplot.scatter(x=data[feat], y=data[target])
_ = pyplot.xlabel('feat')
_ = pyplot.ylabel('target')
pyplot.show()

Note that unlike MATLAB, semi-colons don't suppress the output in python, they are simply used to delimit different statements, and are generally unnecessary if you are using newlines as delimiters (which is the standard way to do it)

sacuL
  • 49,704
  • 8
  • 81
  • 106