Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib打印动画二维网格_Python_Python 3.x_Matplotlib_Plot - Fatal编程技术网

Python 使用matplotlib打印动画二维网格

Python 使用matplotlib打印动画二维网格,python,python-3.x,matplotlib,plot,Python,Python 3.x,Matplotlib,Plot,我需要创建一个函数来使用Python 3.5+Matplotlib绘制动画2D有限元。基本上,我有节点坐标和它们的连通性,以及不同时刻节点值的矩阵: 值[i,j]:是第j时刻第i个节点的节点值 我在网上找到了很多例子(比如),但没有一个能真正帮助我解决问题 这是我编写的代码。它会在一个瞬间打印网格: import numpy as np import matplotlib matplotlib.use('TkAgg') #--- from matplotlib import pyplot as

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

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

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

这是我编写的代码。它会在一个瞬间打印网格:

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)
这里有谁能告诉我如何创建动画绘图功能吗


提前谢谢大家,

您有什么问题?有很多动画的例子。我无法想象没有人能帮助你。无论如何,你应该提到哪个例子对你没有帮助,并说明原因。您还应该更清楚地了解所需的绘图应该是什么样子,以及它与
值[i,j]
的关系。(你有一个二维网格,但如果
j
是时间步长,那么绘图将仅为1D?)。我想我很清楚,但无论如何,只是编辑了这篇文章。