Interactive Plotting

When generating geometry from scratch within MAPDL, it is often necessary to plot the geometry (e.g. keypoints, lines, areas, volumes), and PyMAPDL supports plotting basic CAD using VTK. The Mapdl class leverages the existing MAPDL commands (e.g. Mapdl.kplot(), Mapdl.vplot(), Mapdl.eplot()) and transfers the geometry to Python to visualize it. Combined with the MAPDL geometry commands, you can generate and visualize geometry from scratch without opening the GUI using open_gui().

Line Plotting

Plot lines within Python using Mapdl.lplot().

>>> from ansys.mapdl.core import launch_mapdl
>>> mapdl = launch_mapdl()

Create a rectangle with a few holes

>>> mapdl.prep7()
>>> rect_anum = mapdl.blc4(width=1, height=0.2)

Create several circles in the middle in the rectangle

>>> for x in np.linspace(0.1, 0.9, 8):
...    mapdl.cyl4(x, 0.1, 0.025)

Generate a line plot

>>> mapdl.lplot(color_lines=True, cpos='xy')
../_images/lplot_vtk.png

Line Plot from MAPDL using PyMAPDL and pyvista

Area and Volume Plotting

More complex geometry can be obtained using boolean operations and can be visualized using Mapdl.vplot(). In this example, we cut the initial area with the 8 circles and then extrude it.

>>> plate_holes = mapdl.asba(rect_anum, 'all')

Extrude this area

>>> mapdl.vext(plate_holes, dz=0.1)
>>> mapdl.vplot()
../_images/vplot_vtk.png

Volume Plot from MAPDL using PyMAPDL and pyvista

Node and Element Plotting

Nodes and elements can also be plotted directly from the instance of Mapdl. We continue the example by defining some element types, meshing, and displaying the mesh.

>>> mapdl.et(1, 'SOLID186')
>>> mapdl.vsweep('ALL')
>>> mapdl.esize(0.1)
>>> mapdl.eplot()
../_images/eplot_vtk.png

Element Plot from MAPDL using PyMAPDL and pyvista

Plotting Non-Interactively using MAPDL

You can also plot using MAPDL’s native plotting tools. To use the native tools, pass vtk=False when running plotting commands such as Mapdl.aplot, Mapdl.eplot, etc.. Plots will be generated within MAPDL and then shown using matploblib.

Start PyMAPDL

>>> from ansys.mapdl.core import launch_mapdl
>>> mapdl = launch_mapdl()

Create a square area using keypoints

>>> mapdl.prep7()
>>> mapdl.k(1, 0, 0, 0)
>>> mapdl.k(2, 1, 0, 0)
>>> mapdl.k(3, 1, 1, 0)
>>> mapdl.k(4, 0, 1, 0)
>>> mapdl.l(1, 2)
>>> mapdl.l(2, 3)
>>> mapdl.l(3, 4)
>>> mapdl.l(4, 1)
>>> mapdl.al(1, 2, 3, 4)

Set the view to "isometric"

>>> mapdl.view(1, 1, 1, 1)
>>> mapdl.pnum('kp', 1)  # enable keypoint numbering
>>> mapdl.pnum('line', 1)  # enable line numbering

Each of these will create a matplotlib figure and pause execution

>>> mapdl.aplot(vtk=False)
>>> mapdl.lplot(vtk=False)
>>> mapdl.kplot(vtk=False)
../_images/aplot.png

Area Plot from MAPDL displayed using matploblib

For more details on the plotting functions, see Plotting.

Plotting Keyword Options

When using vtk=True (default True), all the MAPDL plotting methods allow you to enter in additional keyword arguments to better control the plot. For example, you can automatically generate a screenshot of an area plot or element plot with:

>>> mapdl.aplot(savefig='aplot.png')
>>> mapdl.eplot(savefig='eplot.png')

You can also control the camera position with the cpos when you want to view from a standard viewing direction (for example view the XY plane with cpos='xy'.

>>> mapdl.eplot(cpos='xy')

For all general plotting options, see ansys.mapdl.core.plotting.general_plotter().