绘制 3D 网格:
Plotting a 3D Meshgrid:
我正在使用 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]]
这些是 shell 向我吐槽的 U 的许多值。如您所见,我将处理 600 个不同的数组,因为矩阵设置为在特定时间和位置查找 U。总共需要 600 个时间步。
请尝试使用搜索功能;您可以在 Overflow 上的绘图表面上的许多其他线程中找到工作代码 here。
我想您可能在这里混淆了一些术语。您有一个值矩阵,对应于二维函数的值;这不是网格。在 matplotlib
中,您可以通过多种方式可视化三维矩阵,通常作为 surface
, a wireframe
, or an image
(作为热图)。
函数 np.meshgrid()
为您提供 N
维索引。您的 n
和 m
向量需要这个,而不是矩阵 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)
现在创建函数 U(x,t)
:
>>> U = np.matrix(x[:, np.newaxis] * t) # broadcast
>>> U.shape
(3600, 6000)
要绘制曲面,您可以使用 matplotlib
中 Axes3D
中的 surf
函数,但您也可以使用上面链接的线框或图像方法。
>>> 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()
我正在使用 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]]
这些是 shell 向我吐槽的 U 的许多值。如您所见,我将处理 600 个不同的数组,因为矩阵设置为在特定时间和位置查找 U。总共需要 600 个时间步。
请尝试使用搜索功能;您可以在 Overflow 上的绘图表面上的许多其他线程中找到工作代码 here。
我想您可能在这里混淆了一些术语。您有一个值矩阵,对应于二维函数的值;这不是网格。在 matplotlib
中,您可以通过多种方式可视化三维矩阵,通常作为 surface
, a wireframe
, or an image
(作为热图)。
函数 np.meshgrid()
为您提供 N
维索引。您的 n
和 m
向量需要这个,而不是矩阵 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)
现在创建函数 U(x,t)
:
>>> U = np.matrix(x[:, np.newaxis] * t) # broadcast
>>> U.shape
(3600, 6000)
要绘制曲面,您可以使用 matplotlib
中 Axes3D
中的 surf
函数,但您也可以使用上面链接的线框或图像方法。
>>> 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()