matplotlib.pyplot hangs up at any call - python-2.7

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

Related

Matplotlib Pyplot breaks on Ubuntu 16.04

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!

Pyinstaller with Tkinter Matplotlib numpy scipy

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

Cannot close plot generated with matplotlib in ipython notebook

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()

Matplotlib Basemap Installation Troubles on OS X Yosemite

I've been having very serious issues installing Basemap from mpl_toolkits. Fortunately, it looks like I'm not the only one having these issues... I was wondering if someone could help me here.
I'm currently running OS X 10.10 (Yosemite).
Long story short, I was able to install Basemap via Anaconda after much difficulties. Now whenever I try to import Basemap, I get the following error:
28 from matplotlib.lines import Line2D
29 from matplotlib.transforms import Bbox
---> 30 from mpl_toolkits.basemap import pyproj
31 from mpl_toolkits.axes_grid1 import make_axes_locatable
32 from matplotlib.image import imread
ImportError: cannot import name pyproj
...which doesn't make sense to me at all. I have pyproj installed, and I physically see it in my Anaconda pkgs folder. It's called pyproj-1.9.3-0. I also have basemap-1.0.7-np19py27_0 in my pkgs folder.
Does anyone have any idea what's going on? Maybe I stupidly forgot to do something? Sorry if it's a dumb question - I'm an amateur when it comes to these sorts of things.
I just managed to install Basemap on Yosemite with Anaconda without any problems, so maybe there's something unusual about your set-up (what issues are you refering to?)
What I did was simply
conda install Basemap
But here are some things to think about:
Did you install anything after issuing this command (on my system, conda updated my matplotlib to 1.4.3 as part of this installation. You could try conda update, but you might need to do some fixing if you changed anything "by hand".
Are you using the right virtual environment (the one from which you installed Basemap)?
import Basemap with from mpl_toolkits.basemap import Basemap rather than the import Basemap that your question implies: see e.g. this example.
type which python and check that the output is along the lines of
/Users/<username>/anaconda/bin/python
or
/Users/<username>/anaconda/envs/<virtual-env>/bin/python
and that you're not using your Mac's system Python which would be something like
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python

matplotlib plots do not show on Anaconda for Linux

I've read these and tried the solutions but still not getting anything to show:
matplotlib show nothing although i called show
Why doesn't pyplot.show() work? [duplicate]
matplotlib does not show my drawings although I call pyplot.show()
I'm using Python 2.7.6 |Anaconda 1.9.0 (64-bit) on Ubuntu 12.04.
How can I go about diagnosing the problem and trying to solve it?
EDIT:
-First I edited matplotlibrc and change the backend to GtkAgg (was set to QtAgg).
-Then I tried to change the backend via code: matplotlib.rcParams['backend'] = "GtkAgg"
EDIT2 - adding detail from the Spyder console (no plot is shown at the end):
Python 2.7.6 |Anaconda 1.9.0 (64-bit)| (default, Jan 17 2014, 10:13:17)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.8.0, SciPy 0.13.3, Matplotlib 1.3.1
Type "scientific" for more details.
>>> scientific
This is a standard Python interpreter with preloaded tools for scientific
computing and visualization. It tries to import the following modules:
>>> import numpy as np # NumPy (multidimensional arrays, linear algebra, ...)
>>> import scipy as sp # SciPy (signal and image processing library)
>>> import matplotlib as mpl # Matplotlib (2D/3D plotting library)
>>> import matplotlib.pyplot as plt # Matplotlib's pyplot: MATLAB-like syntax
>>> from pylab import * # Matplotlib's pylab interface
>>> ion() # Turned on Matplotlib's interactive mode
Within Spyder, this interpreter also provides:
* special commands (e.g. %ls, %pwd, %clear)
* system commands, i.e. all commands starting with '!' are subprocessed
(e.g. !dir on Windows or !ls on Linux, and so on)
>>> plot(range(3))
[<matplotlib.lines.Line2D object at 0x3f3cd50>]
>>> show()
I ended up solving by doing this in Spyder:
Preferences -> Console -> External modules -> check Install Spyder's input hook for Qt
In order for your matplotlib backend choice to work, you need to have the Python bindings for that backend installed. You may be running a GTK-based windows manager like GNOME, for example, but still not have the Python bindings around. According to this, the proper Ubuntu package is python-gtk. So, open up your favorite Terminal emulator and enter
sudo apt-get install python-gtk
(you'll need admin privileges to do it). Restart your python/matplotlib session, and you should be good to go.
I'm on Mavericks, solved the problem by changing backend to Qt4Agg in matplotlibrc. (as instructed in another answer here: Why doesn't pyplot.show() work?)