How do I omit matplotlib printed output in Python / Jupyter notebook? [duplicate]

ε祈祈猫儿з 提交于 2021-02-06 10:14:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!