Plots won't show up in qtconsole - python

I have read tons of questions on this, but I am still baffled. I'm running Anaconda in Scientific Linux. I launche a console a type ipython qtconsole.
My script.py is something like
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5,6,7,8,9,10])
plt.show()
I type run script.py from the qtconsole, and the program just sits and does nothing. What am I doing wrong? I have been using the qtconsole for running my scripts, maybe it's better for really being interactive with and I should be running my scripts some other way?
Any general advice on workflow would be very helpful here. When should I use python script.py, when should I use ipython script.py, and when should I use the qtconsole, etc...?

This seems like a Magic Function problem
More specifically %matplotlib. %matplotlib inline will show it within the browser, or you can replace inline with whatever you feel better for your use.

Related

IPython notebook: debugging by stopping the execution at specific point

i want to interactively debug code that is deeply hidden in some class.
I know that it is possible open the ipdb using
from IPython.core.debugger import Pdb
Pdb().set_trace()
This work also in ipython (jupyter) notebook.
Another possibility that I find very convenient when I am working in the terminal is to open a shell at a specific point using
from IPython import embed
embed()
and then use ipython directly to interact with variables etc.
However, if I use this in notebook, I get a ipython shell embedded into the notebook, and this shell does not even work well (e.g., if I use print, the kernel blocks).
What I would find most convenient is to interrupt the kernel execution at some predefined point in the code and keep all current variables, so that I can use another cell for debugging and the possibility to, e.g., develop and test new code with the current variables.
Is there some way to achieve this?

Cannot get matplotlib graphics to show up inline

I would like to load automatically the command lines with Jupyter in Ubuntu 16.10 :
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
With Ubuntu, we could normally use configuration files as .vimrc, .bashrc and so on to automate some command lines. I know we could create the config file jupyter_notebook_config.py with jupyter notebook --generate-config. How could we implement that in python in that .py file?
Thanks!
You can't have graphics inline in a regular terminal because regular terminals don't have graphics capabilities. However, if you would be willing to use an alternative console, Jupyter Qtconsole, may be suitable for you.
EDIT: okay, I've had a look and you need to look at this, specifically the parts about running files/lines of code at startup. (like c.InteractiveShellApp.exec_lines or c.InteractiveShellApp.exec_files

What's the best practice in the workflow of writing and running python files with Vim?

Currently, I write pythons files in Vim, and run it with jupyter qtconsole. The advantage of this way is that I could work with Vim so get all the benefits of Vim.
I could run the python directly in Vim using the pymode plugins, but in this way I cannot see and manipulate the output variables, and the figures are opened in another window which is quite annoying when I have to close them to make Vim responsible again. Compared with this, in jupyter qtconsole I could use %maplotlib inline to display figures elegantly.
However, my current workflow has a big disadvantage that every time I run my python script in qtconsole, and then I edit my python script, it is not so easy to run it again with the modified script. Since the module has been loaded, rerun it will not automatically reload the modified module source. I found no easy way to overcome this drawback. Currently I have to restart the kernel and then reset the path, turn on %matplotlib inline, and %run python-script.py again.
Any one can give me a solution?
I find an answer which solves my problem by using ipython extension autoreload.
%load_ext autoreload
%autoreload 2
Then I do not have to restart kernel any more.

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.

Emacs Python-inferior shell not showing prompt after matplotlib show() command

So I've been experimenting with numpy and matplotlib and have stumbled across some bug when running python from the emacs inferior shell.
When I send the py file to the shell interpreter I can run commands after the code executed. The command prompt ">>>" appears fine. However, after I invoke a matplotlib show command on a plot the shell just hangs with the command prompt not showing.
>>> plt.plot(x,u_k[1,:]);
[<matplotlib.lines.Line2D object at 0x0000000004A9A358>]
>>> plt.show();
I am running the traditional C-python implementation. under emacs 23.3 with Fabian Gallina's Python python.el v. 0.23.1 on Win7.
A similar question has been raised here under the i-python platform: running matplotlib or enthought.mayavi.mlab from a py-shell inside emacs on windows
UPDATE: I have duplicated the problem on a fresh instalation of Win 7 x64 with the typical python 2.7.2 binaries available from the python website and with numpy 1.6.1 and matplotlib 1.1.0 on emacs 23.3 and 23.4 for Windows.
There must be a bug somewhere in the emacs shell.
I think there are two ways to do it.
Use ipython. Then you can use -pylab option.
I don't use Fabian Gallina's python.el, but I guess you will need something like this:
(setq python-shell-interpreter-args "-pylab")
Please read the documentation of python.el.
You can manually activate interactive mode by ion
>>> from matplotlib import pyplot as plt
>>> plt.ion()
>>> plt.plot([1,2,3])
[<matplotlib.lines.Line2D object at 0x20711d0>]
>>>
You can use different back-end:
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Other GUI backends:
TkAgg
WX
QTAgg
QT4Agg
If you are using Elpy run your code using C-u C-c C-c
I think that this might have something to do with the behavior of the show function:
matplotlib.pyplot.show(*args, **kw)
When running in ipython with its pylab mode, display all figures and
return to the ipython prompt.
In non-interactive mode, display all figures and block until the
figures have been closed; in interactive mode it has no effect unless
figures were created prior to a change from non-interactive to
interactive mode (not recommended). In that case it displays the
figures but does not block.
A single experimental keyword argument, block, may be set to True or
False to override the blocking behavior described above.
I think your running into the blocking behavior mentioned above which would result in the shell hanging. Perhaps try running the function as: plt.show(block = False) and see if it produces the output you expect. If this is still giving you trouble let me know and I will try and reproduce your setup locally.
I think I have found an even simpler way to hang the inferior shell but only when pdb is invoked. Start pdb by supplying 'python' as the program to run.
Try this code:
print "> {<console>(1)<module>() }"
Well after a tremendous amount of time and posting the bug on the matplotlib project page and the python-mode page I found out that supplying the arguments console --matplotlib in ipython.bat will do the trick with matplotlib 1.3.1 and ipython 1.2.0
This is what I have in my iphython.bat
#python.exe -i D:\devel\Python27\Scripts\ipython-script.py console --matplotlib %*

Categories

Resources