Gre*_*ice 0 numpy matplotlib python-2.7 spyder
我正在使用 Spyder Interface (Python 2.7) 来数值求解 PDE。我将代码设置为根据位置和时间计算 U 的位置。U 是我代码中的 [nxm] 矩阵,其中 n 是位置,m 是时间。因此,在每个 U[n,m] 处,代码在第 m 次给出第 n 个位置。有没有办法可以利用这样的矩阵在 python 中制作网格图。我已经搜索过,但例如 numpy 的网格只处理数组。谢谢你。
[[ 1.20643447 1.20520185 1.20397894 ..., 1.04589795 1.04587534 1.04585286]
[ 1.40901699 1.40658211 1.4041664 ..., 1.09172525 1.09168043 1.09163586]
[ 1.6039905 1.6004133 1.59686428 ..., 1.13741248 1.13734625 1.1372804 ]...,
[ 2.3960095 2.3995867 2.40313572 ..., 2.54969453 2.55003659 2.55037764]
[ 2.59098301 2.59341789 2.57981471 ..., 2.59750546 2.59785406 2.59820163]
[ 2.79356553 2.74473913 2.71231633 ..., 2.64640578 2.64675767 2.64710852]]
Run Code Online (Sandbox Code Playgroud)
这些是贝壳向我吐出的许多 U 值。如您所见,我将处理 600 个不同的数组,因为矩阵设置为在特定时间和位置查找 U。总共需要 600 个时间步。

请尽量使用搜索功能;您可以在此处的许多其他线程中找到工作代码here on 绘图表面here on Overflow。
我想你可能在这里混淆了一些术语。您有一个对应于二维函数值的值矩阵;这不是网格。在中,matplotlib您可以通过多种方式可视化一个 3 维矩阵,通常作为 a surface、 awireframe或 an image(作为热图)。
该函数np.meshgrid()为您提供N维数索引。您需要将其用于您的n和m向量,而不是您的 matrix U,以将它们转换为与您的矩阵形状相同的多维数组。对你来说幸运的是,matplotlib不在乎U是矩阵还是向量。
例如np.meshgrid():
>>> t = np.linspace(0,60,6000)
>>> x = np.linspace(0,2*np.pi,3600)
>>> T, X = np.meshgrid(t, x)
>>> t.shape
(6000,)
>>> x.shape
(3600,)
>>> T.shape
(3600, 6000)
>>> X.shape
(3600, 6000)
Run Code Online (Sandbox Code Playgroud)
现在创建一个函数U(x,t):
>>> U = np.matrix(x[:, np.newaxis] * t) # broadcast
>>> U.shape
(3600, 6000)
Run Code Online (Sandbox Code Playgroud)
并且要绘制曲面,您可以使用例如from 中的surf函数,但您也可以使用上面链接的线框或图像方法。Axes3Dmatplotlib
>>> import matplotlib.pyplot as plt
>>> import pylab
>>> from mpl_toolkits.mplot3d import Axes3D
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> surf = ax.plot_surface(X,T,U)
>>> plt.show()
Run Code Online (Sandbox Code Playgroud)