Jupyter shows plot without plt.show() - python

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?

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.

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

Related

Can %matplotlib notebook only be bused in Jupyter?

I have this and it works in jupyter in browser:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy as sp
However, after I copied it to a python file in the PyDev IDE editor, it always complains the first line:
%matplotlib notebook
Error message:
Encountered "%" at line 1, column 1. Was expecting one of: <EOF> <NEWLINE> ... "(" ... "{" ...
Does this mean the magic method can only be used in jupyter notebook?
Yes, It's a jupyter notebook command.
Well, you can't use it in a file (as that's not valid Python syntax), but you can use it in the PyDev interactive console: http://www.pydev.org/manual_adv_interactive_console.html if you have IPython properly installed.
The command in question, %matplotlib notebook, can be used to specify how to display graphs in a Jupyter notebook. If you use %matplotlib inline, the graphs will be displayed as a part of the Jupyter notebook. Using the form of the command that you asked about will display the graph in an interactive form within the Jupyter notebook. Or using %matplotlib gtk will open another program to display each individual graph, which is how Python would display the graphs in any other situation. So the command does not have any use or meaning outside of a Jupyter notebook.

matplotlib interactive… isn't

I'm running Python 3.5.1 with matplotlib version 1.5.1 and iPython 5.0.0. I can't seem to get matplotlib's interactive feature working. I can run a command to create a plot:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1,2,3])
This doesn't show a figure until I manually execute plt.show(), at which point iPython hangs until I close the figure window. I have interactive set to True in my matplotlibrc file.
It's been a year or so since I used matplotlib. The last time I used it, I got interactive without having to execute plt.show(). Has something changed or am I doing something wrong?
You were probably using interactive mode previously.
Start ipython with:
ipython --pylab
Then your plots will show up instantly.

Plot plotted inside the ipython console

I am working in python and using Anaconda with Spyder and I need to plot a scatter plot in a part of the code I am working on. The plot is just fine but is drawn in the console itself and the size is quite small for my needs.
My question is, how can I plot it in another window outside the ipython console? I already tried changing some of the setting like changing Graphics setting in ipython from inline to QT to tkinter. Nothing seems to work.
Any helpful suggestions?
you can just save %matplotlib qt in a .py file and you can start ipython using:
ipython -i myfile.py

use ipython %matplotlib inline inside python script

I'm writing plot functions in python scripts and using ipython to show them. I want to show the figures inside the page ,so that I used
%matplotlib inline
After that I can show the figures inside.
Is there a way to put this line inside the plot.py script so in ipython I just import the .py without specifying the %matplot code?
Thanks.
You should only have to run %matplotlib inline once per IPython session. All it does it tell matplotlib to plot graphs inline, as opposed to in a separate window.
Running it more than once (say once per script) seems pointless as it won't actually do anything. You could just run it at the top of your session and then not worry about it, your plots will still plot and you can still have plotting functions inside the Python files.
That aside, adding it inside a script is just not possible. It just returns a SyntaxError because you effectively run it as a Python script, not in the IPython terminal where it's set up to handle the magic methods.

Plot inside Ipython Notebook

I'm using IPython notebooks to share code and (hopefully) graphics with m collaborators. sadly, I cannot get matplotlib to plot inside the notebook, it always gives me a pop-up window, which I obviously cannot include in notebook pastes or similar.
Here's two minimal examples of how I go about plotting things.
Either with invoking plt.show() , or without it.
this either gives me an external pop-up, or plots nothing.
You need to be using the matplotlib inline backend.
%matplotlib inline

Categories

Resources