How to use matplotlib and numpy in Mechanical?
Best Answers
-
By activating CPython console in Ansys Mechanical v2021R1 - How do I use CPython Console in Mechanical Scripting?
Ideally you should be able to directly use
matplotlib
andnumpy
in Mechanical console as they are shipped with the default Ansys Workbench v2021R1. Check thesite-packages
in your installation folder:C:\Program Files\ANSYS Inc\v211\commonfiles\CPython\3_7\winx64\Release\python\Lib\site-packages
But I got the following error when trying to do a 3D plot with
matplotlib
in Mechanical.Can't find a usable init.tcl in the following directories: ... This probably means that Tcl wasn't installed properly
This means that
tcl
andtk
libraries weren't installed properly. To get around this issue, one can download the exact CPython version shipped with Ansys WB 2021R1 (https://www.python.org/downloads/release/python-378/)And copy the
tcl
andtk
folders from your local python installation folder:...Python37_64\tcl
to
C:\Program Files\ANSYS Inc\v211\commonfiles\CPython\3_7\winx64\Release\python\Lib
and then restart Mechanical.
0 -
Now even easier in 2022r2. If you set the beta options and enable Cpython options you will be able to set the Engine Type in a Python Code object.
When you have a Cpython code object, then matplotlib will work without issue:
import matplotlib.pyplot as plt plt.plot([1,2,3,4]) plt.ylabel('some numbers') plt.show()
This should by default generate an external matplotlib image:
2
Answers
-
In 22R2 & 23r2, I am not able to see the Engine with bita option as well
0 -
Hello @Mahesh_G , one would need to go to WB --> Tools--> Options and activate the below Beta option and reopen Mechanical to have this Beta option in Mechanical in Python Code object.
0 -
And what do you do when this option is not available? (2023 R2)
0 -
Hello @Anon , the picture indicates that the general beta options box is not checked.
Could you check this box here , click okay and try again to set cpython capability?
0 -
Yes thank you that did it.
1