Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide <matplotlib.lines.Line2D> in IPython notebook

I am plotting a NumPy array of values, I, using IPython notebook in %matplotlib inline mode with the plot command plt.plot(I,'o').

The resulting output is:

<matplotlib.figure.Figure at 0x119e6ead0>
Out[159]:
[<matplotlib.lines.Line2D at 0x11ac57090>,
 <matplotlib.lines.Line2D at 0x11ac57310>,
 <matplotlib.lines.Line2D at 0x11ac57510>,
 <matplotlib.lines.Line2D at 0x11ac57690>,
 <matplotlib.lines.Line2D at 0x11ac57810>,
 <matplotlib.lines.Line2D at 0x11ac57990>,
 <matplotlib.lines.Line2D at 0x11ac57b10>,
 ....
 ....
]

Then my plot shows up below these lines of output.

Is there a way to just show the plot and hide the <matplotlib.lines.Line2D at ...> from the output?

like image 863
ROBOTPWNS Avatar asked Sep 11 '14 14:09

ROBOTPWNS


People also ask

What can I use instead of %Matplotlib inline?

show() . If you do not want to use inline plotting, just use %matplotlib instead of %matplotlib inline .

What happens if I dont use %Matplotlib inline?

In the current versions of the IPython notebook and jupyter notebook, it is not necessary to use the %matplotlib inline function. As, whether you call matplotlib. pyplot. show() function or not, the graph output will be displayed in any case.


2 Answers

You can use a semi-colon ; to end the line. This suppresses the unwanted output when generating plots:

plt.plot(I,'o');

In general, using a semi-colon stops IPython from printing any output value from that line of a code block. For example, the executing the cell containing the code 1+1; would not output 2.

An alternative way would be to bind a variable to the plot:

_ = plt.plot(a)

This way, IPython only shows you the plots and the name _ is bound to the unwanted output.

like image 58
Alex Riley Avatar answered Oct 16 '22 23:10

Alex Riley


Another way is to just write plt.show() at the end of your drawing code. It would take less symbols to type if you're generating many subplots and/or drawing many plots on a single subplot.

like image 23
kurtosis Avatar answered Oct 16 '22 23:10

kurtosis