How to use matplotlib and numpy in Mechanical?

Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 389
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:

Answers

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 389
    First Anniversary Ansys Employee Solution Developer Community of Practice Member First Answer
    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: 199
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    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