2017-05-06 64 views
0

我嘗試使用matplotlib庫來繪製梁的應力。如何使用matplotlib繪製2D漸變(彩虹)?

我已通過使用公式和繪製它用於例如計算值:

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() 

你會看到,我知道所有節點的座標,也元素的應力值。

p.s.對不起,我的語法,我不是本地人。

謝謝。建議。

+0

你試過['pcolormesh'](http://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.pcolormesh.html)([示例](http://matplotlib.org/examples/pylab_examples) /quadmesh_demo.html))? – berna1111

回答

2

在此之後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那裏得到的一樣。