How to plot 2d FEM results using matplotlib?

The Finite Element Method (FEM) is a numerical technique used for solving engineering problems by dividing complex geometries into smaller, simpler elements. Python's matplotlib library provides excellent tools for visualizing 2D FEM results using triangular meshes and contour plots.

Understanding FEM Visualization Components

To plot 2D FEM results, we need three key components:

  • Nodes ? Coordinate points (x, y) that define the mesh vertices
  • Elements ? Triangular connections between nodes (defining the mesh topology)
  • Values ? Scalar values at each node (representing temperature, stress, displacement, etc.)

Basic 2D FEM Visualization

Here's how to create a filled contour plot for FEM results using tricontourf() ?

import numpy as np
import matplotlib.pyplot as plt

# Define mesh nodes (x, y coordinates)
nodes = np.array([
    [0.0, 0.0],
    [1.0, 0.0], 
    [2.0, 0.5],
    [0.0, 1.0],
    [1.0, 1.0],
    [1.7, 1.3],
    [1.0, 1.7]
])

# Define triangular elements (1-based indexing)
elements = np.array([
    [1, 2, 5],
    [5, 4, 1],
    [2, 3, 6],
    [6, 5, 2],
    [4, 5, 7],
    [5, 6, 7]
])

# Node values (e.g., temperature, stress)
values = [1, 2, 1, 2, 7, 4, 5]

# Extract x, y coordinates
x, y = nodes.T

# Create filled contour plot
plt.figure(figsize=(8, 6))
contour = plt.tricontourf(x, y, elements - 1, values, levels=12, cmap='copper')
plt.colorbar(contour, label='Field Value')
plt.title('2D FEM Results Visualization')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.show()
[Displays a filled contour plot showing the FEM mesh with color-coded values]

Adding Mesh Lines

To better visualize the finite element mesh structure, you can overlay the triangular elements ?

import numpy as np
import matplotlib.pyplot as plt

nodes = np.array([
    [0.0, 0.0], [1.0, 0.0], [2.0, 0.5],
    [0.0, 1.0], [1.0, 1.0], [1.7, 1.3], [1.0, 1.7]
])

elements = np.array([
    [1, 2, 5], [5, 4, 1], [2, 3, 6],
    [6, 5, 2], [4, 5, 7], [5, 6, 7]
])

values = [1, 2, 1, 2, 7, 4, 5]
x, y = nodes.T

plt.figure(figsize=(10, 6))

# Create filled contours
contour = plt.tricontourf(x, y, elements - 1, values, levels=12, cmap='viridis')
plt.colorbar(contour, label='Field Value')

# Overlay mesh lines
plt.triplot(x, y, elements - 1, 'k-', alpha=0.3, linewidth=0.5)

# Mark nodes
plt.scatter(x, y, c='red', s=30, zorder=5)

plt.title('2D FEM Results with Mesh Overlay')
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.axis('equal')
plt.show()
[Displays a contour plot with visible triangular mesh lines and node markers]

Key Parameters

Parameter Function Purpose
elements - 1 tricontourf() Convert 1-based to 0-based indexing
levels tricontourf() Number of contour levels
cmap tricontourf() Color scheme ('viridis', 'copper', 'jet')
alpha triplot() Mesh line transparency

Conclusion

Use tricontourf() to create filled contour plots for 2D FEM results. Combine with triplot() to overlay mesh structure and colorbar() to show the value scale for better interpretation.

---
Updated on: 2026-03-26T13:19:02+05:30

941 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements