【问题标题】:Creating a 3D surface plot with matplotlib in python在 python 中使用 matplotlib 创建 3D 曲面图
【发布时间】:2023-04-08 13:12:01
【问题描述】:

我正在尝试绘制 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 上具有不兼容的维度。”

非常感谢任何和所有的帮助。我认为出现错误是因为我将u 定义为Nx(M+1) 矩阵,但有必要让原始程序运行。我不确定如何更正此问题,以便正确绘制图表。谢谢!

【问题讨论】:

    标签: python matplotlib plot mplot3d


    【解决方案1】:

    使用此代码(查看 cmets):

    # 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

    【讨论】:

      【解决方案2】:

      打印出变量xtu的形状很有帮助:

      x.shape == (40,)
      t.shape == (1600,)
      u.shape == (40, 1601)
      

      所以这里有两个问题。 第一个是xt 是一维的,即使它们需要是二维的。 第二个是u 在第二维中比t 多一个元素。 您可以通过运行来修复这两个问题

      t, x = np.meshgrid(t, x)
      u = u[:,:-1]
      

      在创建 3d 绘图之前。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2015-09-13
        • 2016-10-11
        相关资源
        最近更新 更多