使用python中的matplotlib创建3D表面图

哈维尔

我正在尝试绘制3D曲面,但是遇到了一些麻烦,因为该文档的文档matplotlib似乎不够详尽,并且缺少示例。无论如何,我编写的程序都是通过有限差分法以数值方式求解热方程。这是我的代码:

    ## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# Parameters    
L = 1 # length of the rod
T = 10 # terminal time
N = 40 # spatial values
M = 1600 # time values/hops; (M ~ N^2)
s = 0.25 # s := k * ( (dt) / (dx)^2 )

# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N

x = np.arange(x_init, x_end, dx)
x[0] = x_init

# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M

t = np.arange(t_init, t_end, dt)
t[0] = t_init

# time-vector
for m in xrange(0, M):
    t[m] = m * dt

# spatial-vector
for j in xrange(0, N):
    x[j] = j * dx

# definition of the solution u(x,t) to u_t = k * u_xx
u = np.zeros((N, M+1)) # array to store values of the solution

# Finite Difference Scheme:

u[:,0] = x * (x - 1) #initial condition

for m in xrange(0, M):
    for j in xrange(1, N-1):
        if j == 1:
            u[j-1,m] = 0 # Boundary condition
        elif j == N-1:
            u[j+1,m] = 0 # Boundary Condition
        else:
            u[j,m+1] = u[j,m] + s * ( u[j+1,m] - 
            2 * u[j,m] + u[j-1,m] )

这是我为尝试绘制3D表面图而写的内容:

    # for 3D graph
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(x, t, u, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

我在运行代码以绘制图形时收到此错误:“ ValueError:形状不匹配:两个或多个数组在轴1上的尺寸不兼容。”

拜托,任何帮助都非常感激。我认为出现错误是因为我定义uNx(M+1)矩阵,但必须使原始程序运行。我不确定如何更正此问题,以便图形正确绘制。谢谢!

宁静

使用此代码(查看注释):

# plot 3d surface
# create a meshgrid of (x,t) points
# T and X are 2-d arrays
T, X = np.meshgrid(t,x)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Use X and T arrays to plot u 
# shape of X, T and u must to be the same
# but shape of u is [40,1601] and I will skip last row while plotting
surf = ax.plot_surface(X, T, u[:,:1600], rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

结果:

在此处输入图片说明

因为matplotlib的文档似乎不够详尽,并且缺少示例

http://matplotlib.org/examples/mplot3d/index.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何创建3D表面图

来自分类Dev

Python 中的 3D 表面颜色图

来自分类Dev

R中的3D表面图

来自分类Dev

matplotlib中的自定义颜色图用于3D表面图

来自分类Dev

matplotlib中的3d表面

来自分类Dev

matplotlib 3d表面图未显示

来自分类Dev

使用matplotlib的3D表面图,使用数据框列输入数据

来自分类Dev

使用plotly为3D表面图创建自定义轴标签

来自分类Dev

在Matplotlib中的3d表面图上创建自定义轴值

来自分类Dev

在Plotly Python中更改3D表面图的配色方案

来自分类Dev

在Plotly Python中更改3D表面图的配色方案

来自分类Dev

Python-Matplotlib中的3D线图颜色图

来自分类Dev

Python-Matplotlib中的3D线图颜色图

来自分类Dev

如何在matplotlib中旋转3D表面

来自分类Dev

如何在matplotlib中旋转3D表面

来自分类Dev

绘制3D表面图

来自分类Dev

3D表面图放错轴

来自分类Dev

如何使用matplotlib在python中绘制3D密度图

来自分类Dev

使用 python 绘制 3D 表面: raise ValueError("Argument Z must be 2-dimensional.") matplotlib

来自分类Dev

Matplotlib 3d 绘图:在 2 个表面上获取单个颜色图

来自分类Dev

在Matlab中创建3D图

来自分类Dev

创建没有所有点的3d表面图

来自分类Dev

R中带有plot3D或绘图的3D表面图

来自分类Dev

在python中的3D表面上覆盖纹理

来自分类Dev

Matplotlib的3D图

来自分类Dev

python:3D表面图的2D透视投影

来自分类Dev

python:3D表面图的2D透视投影

来自分类Dev

Python中的3D图

来自分类Dev

在python 3D表面覆盖纹理