使用matplotlib绘制动画2D网格

时间:2017-05-04 23:04:27

标签: python python-3.x matplotlib plot

我需要使用 Python 3.5 + Matplotlib 创建一个绘制动画2D有限元的函数。基本上,我有节点坐标及其连通性和不同时刻的节点值矩阵:

values [i,j]:是第j个时刻第i个节点的节点值。

我在网上找到了很多例子(比如in here),但没有一个能帮我解决问题。

这是我制作的代码。它将网格绘制一个时刻:

import numpy as np
import matplotlib
matplotlib.use('TkAgg')
#---
from matplotlib import pyplot as plt
from matplotlib.collections import PolyCollection
from matplotlib import animation as animation
#---

nelements    = 32
nodesperside = 5
nnodes       = nodesperside*nodesperside
(xmin, xmax) = (0.0, 1.0)
(ymin, ymax) = (0.0, 1.0)
ntimes       = 10

x = np.linspace(xmin, xmax, nodesperside)
y = np.linspace(ymin, ymax, nodesperside)

xx, yy = np.meshgrid(x, y)

xc = np.reshape(xx, (nnodes, ))
yc = np.reshape(yy, (nnodes, ))

nodenum = np.linspace(0,  nnodes - 1, nnodes)

coord       = np.zeros((nnodes, 3))
coord[:, 0] = nodenum
coord[:, 1] = xc
coord[:, 2] = yc

#---
# Conectividade global:
connect = np.zeros((nelements, 4), dtype = 'int')
connect[:, 0] = np.ones((nelements))
connect[ 0, 1: 4] = np.array([0, 1, 6])
connect[ 1, 1: 4] = np.array([1, 2, 7])
connect[ 2, 1: 4] = np.array([2, 3, 8])
connect[ 3, 1: 4] = np.array([3, 4, 9])
connect[ 4, 1: 4] = np.array([0, 6, 5])
connect[ 5, 1: 4] = np.array([1, 7, 6])
connect[ 6, 1: 4] = np.array([2, 8, 7])
connect[ 7, 1: 4] = np.array([3, 9, 8])
connect[ 8, 1: 4] = np.array([5, 6, 11])
connect[ 9, 1: 4] = np.array([6, 7, 12])
connect[10, 1: 4] = np.array([7, 8, 13])
connect[11, 1: 4] = np.array([8, 9, 14])
connect[12, 1: 4] = np.array([5, 11, 10])
connect[13, 1: 4] = np.array([6, 12, 11])
connect[14, 1: 4] = np.array([7, 13, 12])
connect[15, 1: 4] = np.array([8, 14, 13])
connect[16, 1: 4] = np.array([10, 11, 16])
connect[17, 1: 4] = np.array([11, 12, 17])
connect[18, 1: 4] = np.array([12, 13, 18])
connect[19, 1: 4] = np.array([12, 14, 19])
connect[20, 1: 4] = np.array([10, 16, 15])
connect[21, 1: 4] = np.array([11, 17, 16])
connect[22, 1: 4] = np.array([12, 18, 17])
connect[23, 1: 4] = np.array([13, 19, 18])
connect[24, 1: 4] = np.array([15, 16, 21])
connect[25, 1: 4] = np.array([16, 17, 22])
connect[26, 1: 4] = np.array([17, 18, 23])
connect[27, 1: 4] = np.array([18, 19, 24])
connect[28, 1: 4] = np.array([15, 21, 20])
connect[29, 1: 4] = np.array([16, 22, 21])
connect[30, 1: 4] = np.array([17, 23, 22])
connect[31, 1: 4] = np.array([18, 24, 23])
#---

pressao = np.zeros((nnodes, ntimes))
for col in range(ntimes):
    pressao[:, col] = col*np.ones((nnodes))

vertices = list()
for elemento in range(nelements):
    nodes = connect[elemento, 1: 4]
    vertices.append(list(zip(coord[nodes, 1], coord[nodes, 2])))

pi      = pressao[:, 1]
colecao = PolyCollection(vertices, array = pi, edgecolors = 'b')
fig, ax = plt.subplots()
ax.add_collection(colecao)
ax.autoscale_view()
fig.colorbar(colecao, ax = ax)
plt.show()

无论如何,我可以简单地说,就像在Matlab中一样:

% Some initialization     
function framei = plotSolutionTimeVar(p, coord, connect, t0, tf, freq, fn)

niter    = round(fn);
t        = linspace(t0, tf, niter);
nel      = size(connect, 1);
timestep = 1.0/freq;

for ii = 1: niter
    for el = 1: nel
        nodes = connect(el, 2: end);
        xx    = coord(nodes, 2);
        yy    = coord(nodes, 3);
        pp    = p(nodes, ii);
        patch(xx, yy, pp)
    end
    framei(ii) = getframe;
end

end

movie(framei)

请有人在这,请给我一些方向,如何创建动画情节功能?

提前谢谢大家,

0 个答案:

没有答案
相关问题