如何用matplotlib绘制矩形棒上的温度(应力)?

2024-05-16 11:35:38 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试用matplotlib库绘制梁的应力。在

我用公式计算并绘制了一个例子:

Figure 1: Example of before and after of finite element of beam

如图1所示,您将看到绿色光束在元素3和元素8处有更大的应力,因此如果我用彩虹渐变填充颜色,蓝色光束的整体颜色将相同,但绿色光束的颜色将不同,元素3和8将比其他元素更倾向于红色。在

example of stress plot

这是我的一些代码和结果。在

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import numpy as np

node_coordinate = {1: [0.0, 1.0], 2: [0.0, 0.0], 3: [4.018905, 0.87781],
                   4: [3.978008, -0.1229], 5: [1.983549, -0.038322],
                   6: [2.013683, 0.958586], 7: [3.018193, 0.922264],
                   8: [2.979695, -0.079299], 9: [1.0070439, 0.989987],
                   10: [0.9909098, -0.014787999999999999]}
element_stress = {1: 0.2572e+01, 2: 0.8214e+00, 3: 0.5689e+01,
                  4: -0.8214e+00, 5: -0.2572e+01, 6: -0.4292e+01,
                  7: 0.4292e+01, 8: -0.5689e+01}

n = len(element_stress.keys())
x = np.empty(n)
y = np.empty(n)
d = np.empty(n)

for i in element_stress.keys():
    x[i-1] = node_coordinate[i][0]
    y[i-1] = node_coordinate[i][1]
    d[i-1] = element_stress[i]

mask = np.logical_or(x < 1.e20, y < 1.e20)
x = np.compress(mask, x)
y = np.compress(mask, y)
triang = tri.Triangulation(x, y)
cmap = mpl.cm.jet
fig = plt.figure(figsize=(80, 40))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
cax = ax1.tricontourf(triang, d, cmap=cmap)
fig.colorbar(cax)
plt.show()

example plot from my code

你会看到我知道所有的节点坐标,还有元素的应力值。在

但是我的图形的颜色不是平滑的,也不像上面的例子那样水平排列。在

怎么做和它一样?在

对不起我的语法,我不是本地人。在

谢谢。征求意见。在


Tags: importnode元素coordinatematplotlib颜色asnp