Baron Yugovich
Baron Yugovich

Reputation: 4307

Suppress output in matplotlib

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.

Upvotes: 3

Views: 7311

Answers (2)

hsnee
hsnee

Reputation: 542

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

Upvotes: 6

sacuL
sacuL

Reputation: 51335

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)

Upvotes: 4

Related Questions