How to use matplotlib and numpy in Mechanical?

Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 472
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭
edited June 2023 in Structures

How to use matplotlib and numpy in Mechanical?

Tagged:

Best Answers

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 472
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    Answer ✓

    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 and numpy in Mechanical console as they are shipped with the default Ansys Workbench v2021R1. Check the site-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 and tk 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 and tk folders from your local python installation folder: ...Python37_64\tcl

    enter image description here

    to C:\Program Files\ANSYS Inc\v211\commonfiles\CPython\3_7\winx64\Release\python\Lib

    and then restart Mechanical.

  • M
    M Member, Employee Posts: 244
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭
    edited November 2022 Answer ✓

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

    enter image description here

    This should by default generate an external matplotlib image:

    enter image description here

Answers

  • Mahesh_G
    Mahesh_G Member Posts: 7
    First Anniversary First Comment
    **
    edited March 2024

    In 22R2 & 23r2, I am not able to see the Engine with bita option as well

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 223
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭

    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.

  • Anon
    Anon Member Posts: 5
    First Comment
    **

    And what do you do when this option is not available? (2023 R2)

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 223
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭

    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?

  • Anon
    Anon Member Posts: 5
    First Comment
    **

    Yes thank you that did it.