gpt4 book ai didi

python - 如何使用 matplotlib 绘制 2D 梯度(彩虹)?

转载 作者:行者123 更新时间:2023-11-30 22:37:40 33 4
gpt4 key购买 nike

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

我已经使用公式计算并绘制了它作为示例:

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

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

example of stress plot

这是我的一些代码。将 matplotlib.pyplot 导入为 plt 将 matplotlib 导入为 mpl

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}

cmap = mpl.cm.jet

fig = plt.figure(figsize=(8, 2))
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax2 = fig.add_axes([0.05, 0.4, 0.9, 0.15])
# ax1 = fig.add_axes([0.2572e+01, 0.8214e+00, 0.5689e+01, -0.8214e+00, -0.2572e+01, -0.4292e+01, 0.4292e+01, -0.5689e+01])
norm = mpl.colors.Normalize(vmin=0, vmax=1)
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation='vertical')
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, norm=norm, orientation='horizontal')
plt.show()

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

附:抱歉我的语法,我不是母语。

谢谢。寻求建议。

最佳答案

关注此example ,我想这就是您正在寻找的:

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]

triang = tri.Triangulation(x, y)

cmap = mpl.cm.jet
fig = plt.figure(figsize=(8, 4))

ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
ax1.tricontourf(triang, d, cmap=cmap)

ax2 = fig.add_axes([0.05, 0.4, 0.9, 0.15])
x_2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
d_2 = x_2[:]
triang_2 = tri.Triangulation(x_2, y_2)
ax2.tricontourf(triang_2, d_2, cmap=cmap)

fig.show()

enter image description here

添加第二个示例是为了澄清,因为从给定数据获得的图表与从 Comsol 获得的图表不同。

关于python - 如何使用 matplotlib 绘制 2D 梯度(彩虹)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43823686/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com