I am trying to make a plot using matplotlib in ipython notebook.
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
I get the desired plot, but when I try to close it, it hangs. I then have to force quit it, and restart the kernel.
If I run the same code from terminal, I can safely close the plot. The error is when using ipython notebook.
I am on OS X El Capitan (10.11.2), python 2.7. The issue began after updating to El Capitan.
Thanks in advance.
When your work in a notebook, it is better to display the plots inline. Turn on the inline mode:
In: [1] %matplotlib inline
Now, plot without the plt.show():
In: [2]import matplotlib.pyplot as plt
In: [3]plt.plot(range(10))
The plot should appear right in the notebook.
For interactive plots in the notebook use mpld3. Just add these lines to your notebook:
import mpld3
mpld3.enable_notebook()
Related
pyplot hangs up at any call, like this one:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
or this one:
plt.ylabel('some numbers')
One core of the CPU keeps running at 100%.
Any other call using pyplot (plt.*) will give the same result.
I am using ubuntu 16.04.LTS, python 2.7.12 and matplotlib 1.5.3
I reinstalled Ubuntu and it works now. Otherwise nothing helped.
try ipython from anaconda.
What is your PC configuration? 12MHz with 8MB Ram?
Plotting data with matplotlib is an absolute basic task
i have successfully installed pyqtgraph library in python 2.7. I forked latest project from GitHub and then python setup.py install. I am now trying to show plots with it. I open a python terminal and start typing the following:-
import pyqtgraph as pg
import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
All of these commands are successfully interpreted.
But then i run the command to see the plot as:-
pg.plot(x, y, symbol='o')
It outputs:
<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>
And then a windows titled pythonw opens and says 'not responding' and hangs and i am unable to see any output. After long time window crashes and terminal says:
Kernel died, restarting
What could be the error? Should i have installed using .exe?
EDIT:
As pointed out below by titusjan, the problem is with the default Jupyter/Ipython notebook shipping with Anaconda which i have not been able to correct. There must be some installation problem. And i am working on Windows.
pyqtgraph plots functions based on PyQT GUI-based programming. So the task of displaying plots have to be treated as starting a GUI. As told above, when I feed the commands to the IPython terminal, it works out fine:
import numpy as np
import pyqtgraph as pg
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
As noted above, problem starts when i feed the line:
pg.plot(x, y, symbol='o')
To remedy this: either input these 2 lines in one go
pg.plot(x, y, symbol='o')
pg.QtGui.QApplication.exec_()
or immediately after the previous line pg.plot(x, y, symbol='o') input this line:
pg.QtGui.QApplication.exec_()
Alternatively we could use the default QT-GUI commands also. So we get correct plots even if we run this code:-
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import sys
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
However, if one wants to avoid invoking QTGui methods explicitly, assuming one has saved the below code as xyz.py, one can run the code successfully displaying the graphics by writing on the command line: pythonw -i xyz.py. This ensures that python has been explicitly asked to run in interactive mode. pythonw is for running in windows.
import numpy as np
import pyqtgraph as pg
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
I am currently using Python 2.7 on Ubuntu 16.04 and would like the ability to plot figures using Matplotlib. However, calling any sort of plot command causes the entire script to hang at that line.
** Note ** Before marking this as a duplicate question, please consider that this issue may not be related to the backend that I am using as I have tried every iteration of this solution that I have found on the internet.
A simple script which illustrates my problem:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
plt.interactive('True')
if __name__ == '__main__':
print 'Hello World!'
plt.figure()
print 'Hello Again World!'
The output of the above script is: Hello World!
The script then hangs at the plt.figure() line, causes 100% cpu usage, and cannot be killed using Ctrl-C. I use "kill" to kill that process.
I never figured out what caused my problem, but my (poor) solution was to reinstall Ubuntu 16.04 and then matplotlib. It is working fine now.
Thanks everyone!
I tried to do a single plot using Python igraph 0.6.5, cairo 1.8.8 in Ubuntu 14.04.4. This is the code:
import igraph as ig
G = ig.Graph.Full(10)
ig.plot(G, bbox = (400,400), layout = G.layout('kk'))
However, I got this message <igraph.drawing.Plot at 0x7f1a43cc0110> and I do not see the plot (the inline matplotlib is working). I read through this previous post plotting issues of igraph in IPython but the solution seems to be for Python 3. Do you know how to proceed to see the plot. Thanks in advance.
I am using Pyinstaller (after spending a long time with py2exe) to convert my REAL.py file to .exe. I used Anaconda to make .py file which is running perfectly on my computer. But when I make .exe file, it shows no error and an application is created in dist\REAL folder. But when I run the .exe file, the console opens and closes instantly.
It should ideally show a GUI window and take inputs and use them to make plots. It does so when I run REAL.py file. I am using Tkinter, Matplotlib, numpy, scipy which comes with Anaconda.
EDIT: I tried to run simple code to check the compatibility with matplotlib:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The same issue persists with this. Opens console window and then closes but no plot is given out.
I found the solution in py2exe. Following was the setup.py file that worked with Tkinter Matplotlib numpy scipy imports:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
opts = {"py2exe": {
"packages" : ['matplotlib'],
"includes": ['scipy', 'scipy.integrate', 'scipy.special.*','scipy.linalg.*'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
}
setup(
windows = [{'script': "with_GUI.py"}], zipfile = None,
options= opts,
data_files = matplotlib.get_py2exe_datafiles()
)
But this gave me some error saying that there was version conflict with two files. So I changed the two files viz. dist/tcl/tcl8.5/init.tcl (in line 19) and dist/tcl/tk8.5/tk.tcl (in line 18). In my case I changed the version from 8.5.15 to 8.5.18. I found the location of the two files by looking at the path specified by the error in error log. Then the .exe worked just fine.
I hope this helps.
Try using the --hidden-import=matplotlib when calling pyinstaller. For example, in the command prompt you would type:
Pyinstaller --hidden-import=matplotlib your_filename_here.py
and you could try giving it a shot with tkinter in there as well.
Pyinstaller --hidden-import=matplotlib --hidden-import=tkinter your_filename_here.py