How to use matplotlib and numpy in Mechanical?

Options
Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 416
First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
edited June 2023 in Structures

How to use matplotlib and numpy in Mechanical?

Tagged:

Best Answers

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 416
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    Answer ✓
    Options

    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: 230
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited November 2022 Answer ✓
    Options

    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: 6
    First Comment
    edited March 13
    Options

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

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 115
    First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member
    Options

    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.