Need to use TkApp backend with matplotlib in Pyzo - python

I'm trying to put a live mass spectrum plot into a program that I am writing. So far I have a simple GUI that give the program its basic functionality. I also want it to have a live plot of a mass spectrum, a fairly simple 2D bar chart with about 10,000 peaks. I'd like the user to be able to zoom and pan around the plot but the default backend of matplotlib is really slow. I've been reading around and other backends are available and TkApp is recommended. I also want it to be compatible with tkinter Python 3.3.
When I try to change the backend:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy
print(mpl.pyplot.get_backend())
mpl.pyplot.switch_backend('TkAgg')
plot = plt.plot([1, 2, 3, 4, 5, 6])
plt.show(plot)
print(mpl.pyplot.get_backend())
Output:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
Qt4Agg
Traceback (most recent call last):
File "C:\MyPyProgs\Sequencer\spectrum_plot.py", line 7, in <module>
mpl.pyplot.switch_backend('TkAgg')
File "C:\pyzo2013c\lib\site-packages\matplotlib\pyplot.py", line 124, in switch_backend
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\__init__.py", line 28, in pylab_setup
globals(),locals(),[backend_name],0)
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 11, in <module>
import matplotlib.backends.tkagg as tkagg
File "C:\pyzo2013c\lib\site-packages\matplotlib\backends\tkagg.py", line 2, in <module>
from matplotlib.backends import _tkagg
ImportError: cannot import name _tkagg
I'm using the Pyzo package. A couple of recommendations as fixes are to install python3-matplotlib-tk however it cannot find pyzo as it's a stand alone installation of python so is not in the registry. Any help would be much appreciated.
Thanks

First, it sounds like the Pyzo package is missing the pyparsing dependency, so that may be one cause of your speed issues.
Second, from the error message it looks like your matplotlib wasn't compiled with Tk support for some reason. You might want to contact Pyzo and see if they're aware of this.
As a workaround, I'd recommend first installing Python from python.org, then visiting Christoph Gohlke's Python Extension Packages for Windows repository. If you haven't already run across it, it's a very large and very up-to-date collection of scientific and other Python modules for Windows, packaged as self-installing .exe files. Many of the packages are linked to NumPy and SciPy compiled with Intel's Math Kernel Library (MKL), which can give significantly better performance in heavy-duty applications. Another nice thing about it is he gives dependency information where needed, so if you visit the matplotlib link it shows what other packages you'll need to download as well.
While you're there, I'd highly recommend checking out IPython, an interactive Python development environment that is just overflowing with features.

I have managed to fix this with the help of Almar from the Pyzo community group. I'm posting it here to help anyone else who might have the same issue.
So the first thing to do is to add pyzo to the registry, this is done by running pyzo_activate.exe. The cmd is then opened, I then answered y to all of the questions.
After this I downloaded the compatible version of matplotlib (and also PyQtGraph as it was recomended) and they could be installed as normal and recognise pyzo as the python version. now when the code from the question is run the following output is produced:
C:\pyzo2013c\lib\site-packages\matplotlib\mathtext.py:46: UserWarning: Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing has been disabled. Mathtext rendering will be much slower as a result. Install pyparsing 2.0.0 or later to improve performance.
warn("Due to a bug in pyparsing <= 2.0.0 on Python 3.x, packrat parsing "
TkAgg
TkAgg
This means that the default backend of matplotlib is now TkAgg. The pan/zoom controls for plot that is produced are smooth and responsive.

Related

Python does not import installed external library

This is a rather weird problem. I'm on Windows and using Anaconda for Python. There is a financial tool library called TA_lib (Technical Analysis Library). Since this library is not officially supported, I had to install a whl version and I got it from here.
After installation I was able to use it. But sometimes, when I start Anaconda and Spyder, the import talib command yields "Talib has no functiona as ..." and I cannot import and use it. When this happens, I follow these steps:
I close Anaconda and Spyder
I install the library again through Anaconda prompt (I see requirement already satisfied messge)
I restart Anaconda and Spyder
If I'm lucky, I can import talib after first try. If not, I have to repeat these steps for a couple of times. What might be wrong?
EDIT 1:
Now it's not working. Here is the error log:
import pandas as pd
import talib
import numpy as np
Traceback (most recent call last):
File "<ipython-input-2-a3bb601353da>", line 2, in <module>
import talib
File "C:\Users\ismetb\Desktop\AlgoritmikFinans\Classifier 11-12-2018 1830 v2\talib.py", line 90, in <module>
upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=talib.MA_Type.T3)
AttributeError: module 'talib' has no attribute 'BBANDS'
There is a chance you've installed it wrong. Also consider that this is an unofficial library.
Make sure you installed the correct TA_lib for your Python version. What Python version are you using?
If it's 3.4, make sure you have downloaded the TA_lib cp34 for windows 64 bits or 32 bits. If it's another version, make sure you find the one appropriate to you.
Also another useful link to download TA_lib for 64bit is here.
I figured the problem. My Python file name was Talib_pure and then I renamed it to Talib. Since I import talib with import talib command, Python tried to import my own code isntead of talib library and therefore it got confused.
I renamed the file to ta_lib and now it works (so far at least).

creating standalone exe using pyinstaller with mayavi import

I have a program that helps visualize some data in 3D by plotting a surface and a cloud of points to see how they relate to the surface. For the visualization I am using mayavi since it was easy to set up and does a better job than matplotlib in 3D. The program works great when I run it in my python environment and makes beautiful visualizations. However, I need to distribute this to a few people who don't have Python and would prefer not to have to install python and all the add-ins on each computer, so I have been using pyinstaller to create standalone .exe files that they can run after I develop a program.
For reference, this is all done on Windows 10, Python 3.6, pyqt 4.11.4, pyface 6.0.0, traits 4.6.0, pyinstaller 3.3.1, mayavi 4.5.0+vtk81. Just about every module I use was installed using pip.
The problem is that I can't seem to get a working exe if I use/import the mayavi module. I have been reading so much github documentation on different hook files and hidden-imports and each time I fix one error another pops up. It all started with scipy but I believe I have those worked out. So I have a few questions that could help me solve the problem:
1) Has anyone successfully created a standalone exe using pyinstaller with a mayavi import (specifically from mayavi import mlab)? What is your secret?!?
This seems similar but I haven't been able to figure it out yet... SO_link
I have used the following links (scipy,h5py,pandas,traits/qt4,ETS_TOOLKIT) to add hidden imports or fix other issues but I am stuck now after setting my ETS_TOOLKIT=qt4. Before setting it, I would get a pyface/traits error RuntimeError: No traitsui.toolkits plugin found for toolkit null, but now it says the same thing for qt4 instead of null. I have qt4 installed so I don't understand that... It is in the import_toolkit function in pyface/base_toolkit.py.
2) Is there a better route to go in terms of 3D visualization / exe creation? I need a 3D program that can accurately render if the points are in front of or behind the surface and be able to rotate/zoom/pan interactively, plus it needs to be intuitive. Mayavi had very simple commands similar to matplotlib but others seem very complicated working around how the UI interacts with everything.
3) How do I interpret all of these error codes I get? They are usually pretty cryptic saying a certain line threw an exception nested in 10 other function calls and it seems very difficult to back out where exactly things went wrong, especially when nothing pops up on Google that seems to be related.
EDIT
While I am still very confused, I have been able to change where the error occurs. Following the traceback, I commented out a line setting the marker color in traitsui/editors/code_editor.py (line 49), at which point the exception then started the next time the Color method was called... but I still get the same RuntimeError. So that doesn't tell me much other than I am still looking for what hidden import I need to include for this thing to work.
Also note that I get the exact same error with both PyInstaller and cx_Freeze, in case that helps...
Edit 2
I have now tried it using anaconda for python 2.7, SAME EXACT ISSUE..... I'm starting to believe the universe doesn't want this to happen. Is there somewhere else I should bring this issue up?? I have posted on the traitsui GitHub but that wasn't very helpful. This seems to be bigger than pyinstaller/cx_freeze since it happens in both....
I dealt with the same problem and finally switched to cx_freeze, which now works fine on linux and windows. The problems you are dealing with arise from statements like in the SE answer, you found, i.e. dynamic import statements, where what is imported is only determined at runtime:
be = 'pyface.ui.%s.' % tk
__import__(be + 'init')
I couldn't fix that in pyinstaller, while in cx_freeze it works, when you explicitely add the required packages in the build file. Here is the package list I used:
"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pkg_resources._vendor','pkg_resources.extern','pygments.lexers',
'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi']
Here is a full build script that works with python3.6, cx_freeze 5.0.2, mayavi 4.5.0+vtk71, traits 4.6.0, pyface 5.1.0 and traitsui 5.1.0.
import os
from cx_Freeze import setup, Executable
import cx_Freeze.hooks
def hack(finder, module):
return
cx_Freeze.hooks.load_matplotlib = hack
import scipy
import matplotlib
scipy_path = os.path.dirname(scipy.__file__) #use this if you are also using scipy in your application
build_exe_options = {"packages": ["pyface.ui.qt4", "tvtk.vtk_module", "tvtk.pyface.ui.wx", "matplotlib.backends.backend_qt4",'pygments.lexers',
'tvtk.pyface.ui.qt4','pyface.qt','pyface.qt.QtGui','pyface.qt.QtCore','numpy','matplotlib','mayavi'],
"include_files": [(str(scipy_path), "scipy"), #for scipy
(matplotlib.get_data_path(), "mpl-data"),],
"includes":['PyQt4.QtCore','PyQt4.QtGui','mayavi','PyQt4'],
'excludes':'Tkinter',
"namespace_packages": ['mayavi']
}
executables = [
Executable('main.py', targetName="main.exe",base = 'Win32GUI',)
]
setup(name='main',
version='1.0',
description='',
options = {"build_exe": build_exe_options},
executables=executables,
)
I import pyface in the following way:
os.environ['ETS_TOOLKIT'] = 'qt4'
import imp
try:
imp.find_module('PySide') # test if PySide if available
except ImportError:
os.environ['QT_API'] = 'pyqt' # signal to pyface that PyQt4 should be used
from pyface.qt import QtGui, QtCore
before importing mayavi

python script that imports matplotlib succeeds butfrozen binary of script fails

My script needed to import numpy, sklearn, and matplotlib but I couldn't install sklearn. A very helpful response to my question http://https://stackoverflow.com/questions/38733220/difference-between-scikit-learn-and-sklearn explained that I needed to reinstall numpy. Using pip to update numpy failed because OS X 10.11 SIP prevented uninstalling the current numpy. The very helpful answer to a question about pip and SIP by mfripp http://https://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-os-x-el-capitan-upgrade provided a detailed solution to the problem. I followed those instruction exactly and used pip to reinstall numpy, matplotlib, scipy and sklearn for all users.
When I ran my completed script using the command
python DistMatPlot.py Random10A.matrix Random10A.pdf
the script ran perfectly, writing all expected output files.
However, I always saw:
"/Library/Python/2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.')"
which I had never seen with other matplotlib scripts before updating numpy, matplotlib, etc. The 2 second delay was only mildly annoying.
I compiled a frozen binary using pyinstaller and during the compiling I got several messages similar to that above.
The resulting frozen binary run failed with the command:
./DistMatPlot Random10A.matrix Random10A.pdf
produced the following:
/var/folders/8x/7_zp_33h8xj6td0059b72p9h0000gp/T/_MEIhIysTV/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. Traceback (most recent call last): File
"", line 13, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/pyplot.py", line 114, in File "matplotlib/backends/init.py", line
32, in pylab_setup File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 389, in load_module
exec(bytecode, module.dict) File "matplotlib/backends/backend_macosx.py", line 24, in File
"/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py",
line 546, in load_module
module = imp.load_module(fullname, fp, filename, ext_tuple) RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are Working with Matplotlib in a virtual enviroment
see 'Working with Matplotlib in Virtual environments' in the
Matplotlib FAQ DistMatPlot returned -1
I have looked at similar questions and tried their suggested solutions to no avail.
(1) Why does matplotlib need to rebuild a font cache each time it is run?
(2) Why does the frozen binary fail when the script itself succeeds? Do I need some additional option other than -F when running pyinstaller?
The issue with the frozen binary turned out to be "RuntimeError: Python is not installed as a framework."
Several posts discussed that issue and suggested adding these two lines before "import matplotlib.pyplot as plt":
import matplotlib
matplotlib.use('TkAgg')
That did not work, but this slight modification did work:
import matplotlib
matplotlib.use('Agg')
I suspect that 'Agg" may be specific to either OS X or to the version of python that is included with OS X 10.11
Pyinstaller can have problems compiling some scripts, particularly if the use either the scipy or matplotlib modules. Compiling appears to succeed, but the compiled script may fail to execute. If the error message indicates that a module, e,g, scipy.stats, could not be found try compiling with the hiddenimport option:
pyinstaller -F --hiddenimport=scipy.stats --hiddenimport=_sysconfigdata GrowthRates.py
When compiling scripts that use scipy or matplotlib Anaconda python helps a lot:
OSX: python27Env
Windows: Use Anaconda python
Linux: Use Anaconda python

can't import pyd file from scipy

trying to import scipy.interpolate but it can’t import _fblas.pyd. It keeps throwing the following error
File "C:\cygwin64\lib\python2.7\site-packages\scipy/linalg/blas.py", line 155, in <module>
from scipy.linalg import _fblas
ImportError: cannot import name _fblas
My online research suggested that this is because I had the wrong version of numpy installed, and I need to install numpy+mkl. So I uninstalled the version I had and pip installed numpy 1.10.4, which includes mkl.
This did not fix the original problem with _flbas.pyd.
I would reinstall scipy but I can't remember where I got the version I have, and when I pip install scipy 0.17.1.tar.gz it throws an error saying that it has no blas resources, which are necessary. I've also tried to download it from here: https://github.com/scipy/scipy/releases, but they don't include _fblas.pyd in the first place.
I’m not sure where to go from here. I'm not even entirely sure why it won't import the _fblas.pyd file.
This looks like you tried to manually comply to your needed packages. Even though I take my hat off at your effort, keep in mind that this is not an simple task at all.
I might also add that this kind of work is not meant for a developer ("the person who writes the code"), but a full time maintainer. Specially with your particular Cygwin environment
So, what can you do? Alternatively, you can use one of Continuum's mkl-ready python-and-many-tools-including-mkl+scipy distribution or Enthought's Canopy. Hopefully, this will avoid you lots of hours of staring at the screen wondering what's wrong

matplotlib RuntimeError: Python is not installed as a framework

This question has been asked before, in here, also here. However, the solution didn't fix the problem for my case.
The original error is, when I try to import matplotlib.pyplot, I got:
Traceback (most recent call last): File "", line 1, in
File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py",
line 114, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() File
"/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/init.py",
line 32, in pylab_setup
globals(),locals(),[backend_name],0) File "/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py",
line 24, in
from matplotlib.backends import _macosx RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to
function correctly if Python is not installed as a framework. See the
Python documentation for more information on installing Python as a
framework on Mac OS X. Please either reinstall Python as a framework,
or try one of the other backends. If you are Working with Matplotlib
in a virtual enviroment see 'Working with Matplotlib in Virtual
environments' in the Matplotlib FAQ
I followed the solutions to add a ~/.matplotlib/matplotlibrc file with the code: backend: TkAgg. After doing that, my error changed to:
/Users/XX/anaconda/lib/python2.7/site-packages/matplotlib/font_manager.py:273:
UserWarning: Matplotlib is building the font cache using fc-list. This
may take a moment. warnings.warn('Matplotlib is building the font
cache using fc-list. This may take a moment.') objc[25120]: Class
TKApplication is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKMenu is
implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class
TKContentView is implemented in both
/Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined. objc[25120]: Class TKWindow
is implemented in both /Users/XX/anaconda/lib/libtk8.5.dylib and
/System/Library/Frameworks/Tk.framework/Versions/8.5/Tk. One of the
two will be used. Which one is undefined.
I have no idea how to fix that. I'm not using a virtual machine.
PS: I found out that by adding:
import matplotlib
matplotlib.use('TkAgg')
before import matplotlib.pyplot, it seems to work. But adding those two lines of code every time is annoying...
What's going on, and how I can fix it?
I run my script in virtualenv. Python version is 3.5.
Add a line:
backend: TkAgg
in file:
~/.matplotlib/matplotlibrc
This solved the problem.
If you want to know more about why adding this solves the problem, you can read about customizing matplotlib's backend. And TkAgg solves this issue because of it's dependency with Tkinter.
Below worked for me:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
Reference:
https://github.com/tensorflow/tensorflow/issues/2375
I installed Jupyter Notebook in virtualenv and below worked for me:
At Console:
echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
pip install matplotlib==2.1.0
On Notebook:
import matplotlib
Restart the kernel:
dataframe.plot.hist()
...
In my opinion, it will fix the error.
I had the same issues in python 2.7 in virtualenv and I managed to fix those by simply downgrading matplotlib to version 2.1.0
I was also having the same error. What I have done is to install miniconda packages and using the pythonw. Python app was already installed in my mac. Installation is as simple as breaking an egg. Just bash the .sh file in the terminal.
I was using pyenv so the matplotlibrc path wasn't in the home directory, so I created this script to figure the path out and change the backend to Tkagg:
vim $(python -c "import os,matplotlib; print(os.path.join(os.path.dirname(matplotlib.__file__), 'mpl-data/matplotlibrc'));")
This script should work with any python on your system though.

Categories