Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter shows plot without plt.show()

I am using the Jupyter notebook with Python 2.7. Importing matplotlib like this:

%matplotlib inline    
import matplotlib.pyplot as plt

But I have observed one thing. When I use Python in Spyder I always have to use the plt.show() command at the end of the python script in order to see the plots.

In Jupyter I do not need this command in order to see a plot. I do get this error message:

[<matplotlib.lines.Line2D at 0x91615d0>]

but it still makes a plot. Why is that?

like image 662
fossekall Avatar asked Apr 04 '16 09:04

fossekall


People also ask

Is PLT show necessary in Jupyter?

Plotting from an IPython shell Using plt. show() in Matplotlib mode is not required.

How do you display plots in Jupyter notebook?

Jupyter Notebook - Big Data Visualization Tool IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

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.

How to plot in Jupyter Notebook?

Jupyter Notebook - Plotting. IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show () function of plot object is not called. The show () function causes the figure ...

Why are my Matplotlib statements not working in Jupyter?

Sorry, something went wrong. Splitting matplotlib statements into multiple Jupyter input cells may cause nothing to appear. Check if all your statements are in the same cell. Sorry, something went wrong.

How to show the figure in Matplotlib notebook magic?

The show () function causes the figure to be displayed below in [] cell without out [] with number. Now, add plt.show () at the end and run the cell again to see the difference. Note that the %matplotlib notebook magic renders interactive plot.

How to avoid the display of plot in Matplotlib?

To avoid the display of plot we use close () and ioff () methods. We can save plots generated from Matplotlib using matplotlib.pyplot.savefig ().


2 Answers

You turn on the immediate display with %matplotlib inline.

The line:

[<matplotlib.lines.Line2D at 0x91615d0>]

is no error message. It is the return value of the last command. Try adding a ; at the end of the last line to suppress this.

like image 102
Mike Müller Avatar answered Nov 15 '22 01:11

Mike Müller


The requirement of adding %matplotlin inline is no longer needed in the latest jupyter notebooks. It's a by default addition now.

You can change settings in ipython_kernel_config.py for different behaviour

like image 41
Deepak Kr Gupta Avatar answered Nov 15 '22 00:11

Deepak Kr Gupta