I have 3 1D arrays (node x-coordinates, node y-coordinates and Von-Mises stress scalar) exported from an FEA solver.
I want to create 2D contour plots as shown below in Python:
I have managed to create such plot as shown below: Stress plot result
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
for orient in ['top', 'bot', 'side']:
x = []
y = []
z = []
stress = []
with open('data.txt') as file:
for line in file:
cur_line = line.split('\t')
cur_x_old = cur_line[0]
cur_y_old = cur_line[1]
cur_z_old = cur_line[2]
cur_s_old = cur_line[3]
if cur_x_old == 'X Location (mm)':
pass
else:
cur_x = cur_x_old.replace(",",".")
cur_y = cur_y_old.replace(",",".")
cur_z = cur_z_old.replace(",",".")
cur_s = cur_s_old.replace(",",".")
x.append(float(cur_x))
y.append(float(cur_y))
z.append(float(cur_z))
stress.append(float(cur_s))
stress = np.array(stress)
x = np.array(x)
y = np.array(y)
z = np.array(z)
levels=np.linspace(stress.min(), stress.max(), num=100)
triang = tri.Triangulation(x, y)
if orient == 'side':
plt.figure(figsize = (max(x)/50, abs(min(y))/50))
plt.tricontourf(triang, stress, cmap = 'jet', norm = mpl.colors.Normalize(0, 100), levels = levels, extend = 'max')
plt.scatter(x, y, color = 'k')
else:
plt.figure(figsize = (max(x)/50, max(z)*2/50))
plt.tricontourf(x, z, stress, cmap = 'jet', norm = mpl.colors.Normalize(0, 100), levels = levels)
My problem is that by triangulating the data, unwanted triangles are generated at the edge of the mesh (see Stress plot result). The black dots are the scatter plot from x and y coordinates. I want the colour plot to be only inside the boundaries of the grid. Is there a way to remove these unwanted triangles?