问题
When I make a simple plot inside an IPython / Jupyter notebook, there is printed output, presumably generated from matplotlib standard output. For example, if I run the simple script below, the notebook will print a line like: <matplotlib.text.Text at 0x115ae9850>
.
import random
import pandas as pd
%matplotlib inline
A = [random.gauss(10, 5) for i in range(20) ]
df = pd.DataFrame( { 'A': A} )
axis = df.A.hist()
axis.set_title('Histogram', size=20)
As you can see in the figure below, the output appears even though I didn't print
anything. How can I remove or prevent that line?
回答1:
This output occures since Jupyter automatically prints a representation of the last object in a cell. In this case there is indeed an object in the last line of input cell #1. That is the return value of the call to .set_title(...)
, which is an instance of type .Text
. This instance is returned to the global namespace of the notebook and is thus printed.
To avoid this behaviour, you can, as suggested in the comments, suppress the output by appending a semicolon to the end of the line:
axis.set_title('Histogram', size=20);
The other approach would assign the Text
to variable istead of returning it to the notebook. Thus,
my_text = axis.set_title('Histogram', size=20)
sovles the problem as well.
来源:https://stackoverflow.com/questions/45516770/how-do-i-omit-matplotlib-printed-output-in-python-jupyter-notebook