Plot Window Hangs or is Empty in Python - python

I am having trouble with plots in a Jupyter Notebook in Python 3.5 on Mac OSX. The following code will hang when executed:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
myfig = plt.plot(range(5))
plt.show()
If I restart the kernel and un-comment '%matplotlib inline', I do get plots to work inline. However, I'd like to be plotting in a separate window.
If I insert the following code at the beginning:
import matplotlib
matplotlib.use('Agg')
then restart the kernel and run, the code will not hang, but nothing will be plotted, no window opened.
Details:
Mac Book Pro running OSX El Capitan
Anaconda Python 3.5 in a Jupyter Notebook
backend is "MacOSX".

There is a post on GitHub mentioning that using Qt4Agg as backend worked...
If it is not available (and if you can), you might want to try using Hombrew to install Python (instead of Anaconda), Qt and/or Gtk with which you'll be able to use matplotlib without problem.

Related

UnknownBackend: No event loop integration for u'inline' when enable inline matplotlib in emacs python notebook

I am trying to enable the python notebook in emacs. I am following the instruction from this page: https://realpython.com/blog/python/emacs-the-best-python-editor/
When I tried to do "%matplotlib inline" so that I can show the plot inline, I got the following errors:
"UnknownBackend: No event loop integration for u'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, OSX"
My emacs is 24.4 and jupyter notebook version is 4.3
Thanks
If you are running matplotlib on osx this helped resolve my problem:
I upgraded to matplotlib-2.2.0 by now there is a new version 2.2.0
Import matplotlib
calling matplotlib.use('TkAgg') directly after the import of matplotlib.(comments
importing pyplot
call pyplot.plot().
call %matplotlib osx
I had to call %matplotlib osx instead of %matplotlib inlineone of the comment mentiond it
After that it worked.

maplotlib.pyplot.show() works with python but not with jupyter console

I have looked at this question and also this one. It looks like for me, matplotlib.pyplot.show() shows a figure from python, but not from jupyter console.
matplotlib.matplotlib_fname() returns the same matplotlibrc file location for both.
However, when I try to find the backend being used with matplotlib.rcParams['backend'] jupyter console tells me - 'module://ipykernel.pylab.backend_inline', regardless of which backend I have modified the matplotlibrc file to use.
Python on the other hand, correctly shows the backend I'm using; currently 'TkAgg'.
I installed matplotlib using python -mpip install -U matplotlib.
I'm using the following versions:
Windows 10
Jupyter console 5.2.0
Python 2.7.14
IPython 5.5.0
I can make do with using python, but it would be nice to figure this out for jupyter console as well.
First note that plt.show() works as expected, also in Juypter.
This uses the default 'module://ipykernel.pylab.backend_inline' backend. This backend is set by Jupyter, independently of the rcParams setting.
You may set the backend using matplotlib.use()
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
or just using IPython magic %matplotlib backendname
%matplotlib tk
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
You may change the backend using pyplot.switch_backend()
plt.switch_backend("TkAgg")
plt.plot([1,2,3])
plt.show()
or using the same IPython magic
%matplotlib tk
plt.plot([1,2,3])
plt.show()
If you want to set the backend to be used by default, see this question:
Change default backend for matplotlib in Jupyter Ipython

matplotlib 2.1.2 figure windows will not close (manually or via keyboard shortcut) after updating to os x High Sierra

I am analyzing flow cytometry data using python 2.7.12 with the FlowCytometryTools0.4.5 module. After updating to High Sierra (macOS), plot-figure windows will no longer close after being called with the show() funtion. Figures are created with pylab 1.13.3 which was installed with matplotlib 2.1.2 alongside FlowCytometryTools 0.4.5 - all modules were installed using the pip install command in the terminal (ex: python2.7 pip install matplotlib). Python still runs in the background after you attempt to close the window, and scripts will continue to run as if the figure has closed, but the window remains on screen, making future work difficult as the window cannot be minimized or removed without force quitting the Launcher (space ship icon).
As mentioned before, FlowCytometryTools uses pylab, so I was interested if plotting through pyplot would yield a different result, but, alas, it was not to be. Even basic pyplot.plot(s), independent of the FlowCytometry module, had the same problem. This problem did not exist when running the same script(s) and versions of Python, matplotlib, FlowCytometryTools when running on OS X Sierra. Interestingly, this problem does not occur when plotting with pyplot using python 3.6 and matplotlib 2.1.2, but, unfortunately FlowCytometryTools runs only on Python 2.
Both pylab and matplotib use the backend 'MacOSX' - at lease that's what appears when I enter the command pylab.rcParams['backend'].
A simple set of code that produces the un-closable figure window is below:
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
plt.plot(x,y)
plt.show()
After entering the show() command the figure appears, but the window cannot be closed without force quitting the Launcher. Again, I am using Python 2.7.12, matplotlib 2.1.2 and pylab 1.13.3. This was not a problem using the same code when running OS X Sierra, and is not a problem when the same code above is run with Python 3.6 and matplotlib 2.1.2.
If anyone has any ideas please let me know.

matplotlib pyplot Invalid DISPLAY variable

While importing and attempting the following:
import matplotlib
from matplotlib import pyplot as plt
plt.plot([1,2,3],[1,4,9])
plt.show()
I get the following error. How do I fix? I am running Python 2.7, and notebook version 4.1.0. Thank you.
RuntimeError: Invalid DISPLAY variable
When running a jupyter notebook on a server, the server may not even be able to display the plot. The usual solution would be to use a non-interactive backend. In case of a jupyter notebook this would be done by adding
%matplotlib inline
at the top of the notebook, such that graphics are shown as png images.

ipython notebook on linux VM running matplotlib interactive with nbagg

I want buttons and other interactive matplotlib objects to appear from within my ipython notebook.
Here is what I've done:
Installed http://datasciencetoolbox.org, it is a vagrant box with ipython installed and version 1.3.1 of matplotlib.
I needed to upgrade matplotlib to the latest version, because it has this capability to do inline interactive plots. What's new in Matplotlib 1.4.1
I needed to run sudo apt-get install pkg-config and
sudo pip install matplotlib --upgradein order to get that going.
Then, in order to produce the nice (i.e. error-free) screenshot below, I went into the .ipython/dst-profile/ipython_notebook_config.py file and erased the line about IPKernelApp.pylab='inline' to be able to run the matplotlib.use('nbagg') command.
Then I was able to create the screenshot below. However, things still look poor. Those buttons are not buttons. That is an image of buttons. Please advise on how to make those buttons come to life!
Oh... and check this out if this helps you help me.
Thanks!
Basically you are facing two issues
the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.
once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks #tcaswell for the hint)
With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):
%matplotlib nbagg
from matplotlib.widgets import Button
import matplotlib.pyplot as plt
def callback(event):
plt.text(event.xdata, event.ydata, 'clicked')
f,a = plt.subplots(1)
b1 = Button(a,'Button1')
b1.on_clicked(callback)
plt.show()
Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.

Categories

Resources