Using Matplotlib & Abaqus 6.12 - python

I'm currently making a plug-in for abaqus using python. I've searched high and low for a solution to this problem. Ive managed to install matplotlib 1.3.1 into abaqus and its relevent modules (Six, numpy, dateutil, pytz and pyparsing) by simply copying them into the abaqus site-packages folder. It seems Abaqus 6.12 has numpy 1.4 inbuilt but this needs to be updated to 1.5 or later...I simply deleted and replaced it with 1.8.0. I encountered the 'Tkinter' error and resolve it using the following:
import sys
sys.path.append("C:\Python26\Lib\lib-tk")
import Tkinter
import matplotlib.pyplot as plt
Now I can seem to use matplotlib. However whenever I use the plt.plot() function in my plug-in, abaqus freezes. When I shut abaqus down the plot opens soon after. Im not really sure what is going on here, has anyone else experienced this problem or have a potential solution??
I hope this is clear, please ask if I need to elaborate on anything else.

Related

Apps not popping up on macOS Big Sur 11.0.1

It is always risky to upgrade your operation system. It is likely you will encounter some compatibility issue. I took the risk to upgrade my macOS from Catalina to the newest Big Sur. After that, the display in the new OS looks pretty, but all my PyQt5 apps could not be launched in this new OS. The GUI window does not pop up as usual, and there is no error message showing in the terminal. I spent the whole day trying to figure out what makes this problem. I found the solution but in a weird way which I feel confused.
It turns out that the apps comes back to normal after I add the following three lines in the main script.
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
It seems to me the new OS has some compatibility issue with Qt5Agg back-end. But the strange thing is that this solution also works for one of the Pyqt5 app, where I don't use matplotlib at all.
The Python version I used is 3.8.4, and the PyQt5 version I have is 5.15.1.
I hope somebody could explain to me what happen under the hood that makes this solution work. Also I hope this temporary solution can help somebody with the same problem.
A reply to the PyQt mailing list pointed out that setting this env var works:
QT_MAC_WANTS_LAYER=1
Found via Is there any solution regarding to PyQt library doesn't work in Mac OS Big Sur? and https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/?post=29243620#post-29243620
I can confirm that matplotlib.use('TkAgg') followed by matplotlib.use('Qt5Agg') makes things work for me, too. I whittled it down to this as also working:
# from matplotlib.backends import _tkagg
import _tkinter
import matplotlib.pyplot as plt
plt.figure()
So it's something about the compiled _tkinter module. Maybe the inputhook?
As #Eric said, just add the following on the very start of your code, before the PySide2 import:
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
Then import PyQt5/PySide2.
I followed the solution here and downgraded to PyQt 5.13. This solved my issue and allowed my compiled apps to run on Big Sur.
pip install PyQt5==5.13
for me the suggested solution brought a crashes on a breackpoints in pycharm ... the only thing helped :
https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/
all worked as a magic ...
hope QT will fix it soon
I am using macOS Big Sur Version 11.2.2
As suggested by Eric, enter the following line in the terminal before launching your application:
export QT_MAC_WANTS_LAYER=1
This fixed the issue for me!
Adding this to my python program worked for me
import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"

Weird issue when running Python code with data-science libraries

I use Python data science libraries like Pandas, Matplotlib, Numpy. I like working with these. And for ease of use, I also have Anaconda installed which makes creating environments a breeze. But today when running some matplotlib code I face a really weird issue that I didn't encounter ever before. This issue occurs right when I try to import Pyplot.
$ python
>>> import matplotlib.pyplot as plt
!▓KH═⌐-▌ª╚E├╚Hü⌠>┴┘UÜWF♀ε½░±-Sz;½¼öC¿Æ→$√
)ì⌠┐→⌐V¿U!└φÑ^~π┘╠⌡╗♦{ò►ßq╢ê<_│☺♀àV╣oY}ër╡╫╞╟lBîyû,πƒ=♂▒-Öδbùh♂RΣ}Ω¼╘zâ╫♫┘▀\φ→H
$Æ⌡«ε╖#{╤╧↕ƒÅ▄\.╔~s)╥τ♣E:,-Ut⌠↓R↑J¬τpg┐±`&╟╡▓ ▓û
TS1Å`a↑╚+╬╚?└D¿e£▀z¼i♥☼·s&⌐
When I try to import Pyplot I hear three dots (like morse code) and then this gibberish comes up and my program ends. I have never seen anything like this before.
It didn't give me any ModuleNotFoundError or any sort of errors at all. The program just literally stops and the gibberish is printed. I have copied exactly what gets printed.
I have encountered this issue with Matplotlib and also NLTK. Trying to import NLTK shows the same error. I don't know if this issue occurs with any other library but I can confirm it occurs with Matplotlib and NLTK.
I was thinking about re-installing Anaconda but was curious to know more about this really weird issue. I would also like to say that the issue is with my base (root) conda environment, NLTK doesn't have any issues in my other machine-learning environment. Any help is absolutely appreciated.

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

Matplotlib image not coming up in Visual Studio Code on Mac

I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.

How to generate a Cocoa-recognized plot using matplotlib in Python on OS X (Leopard preferably)

I'm not sure exactly what is going on under the hood, but here is my setup, example code, and problem:
setup:
snow leopard (10.6.8)
Python 2.7.2 (provide by EPD 7.1-2)
iPython 0.11 (provided by EPD 7.1-2)
matplotlib (provided by EPD 7.1-2)
example code:
import numpy as np
import pylab as pl
x=np.random.normal(size=(1000,))
pl.plot(x)
problem:
I can't use the standard Mac OS X shorcuts to access the window generated by the plot command.
For example, I can't Command-Tab to the window. Thus, if the window is behind some other window, I need to mouse over to it! Command-W doesn't close it.
Obviously, this is unacceptable. It seems like perhaps running Lion instead of Leopard might fix this, but i haven't upgraded yet. I feel like the problem has something to do with iPython generating windows that aren't fully Cocoa-aware in some sense, but I really know very little so I'm not particularly confident in this hypothesis.
Thus, any ideas on how to either resolve or get around this issue would be much appreciated.
From the description on the iPython page, it looks like Python uses Qt to generate
UI. This means that the windows it generates are definitely not Cocoa windows and will not act like them.
There's not likely to be an easy solution to this issue.
I experienced the same annoyance with my Anaconda installation of Python 2.7.10 on Mac OS X Yosemite 10.10.5. One solution I found was to change the backend to Mac OS X or Qt4Agg by creating a ~/.matplotlib/matplotlibrc file with the line:
backend: MacOSX
or
backend: Qt4Agg
Now I can easily get to the plot window with Application switcher using Command - Tab and close it with Command - W .

Categories