2016-07-12 4 views
1

3D 표면을 플롯하려고하지만 matplotlib의 설명서가 매우 철저하지 않아 예제가 부족하기 때문에 몇 가지 문제가 있습니다. 어쨌든 필자가 작성한 프로그램은 유한 차분 방법을 통해 열 방정식을 수치 적으로 풀어내는 것입니다. 내가 그래프를 플롯 코드를 실행할 때Python에서 matplotlib로 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() 

나는이 오류가 무엇입니까 :

## 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 표면 그래프를 시도하고 음모 쓴 무엇인가 : 여기 내 코드입니다 " ValueError : 모양이 일치하지 않습니다. 두 개 이상의 배열에 축 1에 호환되지 않는 치수가 있습니다. "

모든 도움은 매우 중요합니다. 나는 uNx(M+1) 행렬로 정의했기 때문에 오류가 발생한다고 생각하지만 원본 프로그램을 실행해야합니다. 이 그래프를 올바르게 그리는 방법을 잘 모르겠습니다. 감사!

답변

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() 

결과 :

enter image description here

because the documentation for matplotlib does not appear to be very thorough and is lacking in examples

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

2

이 변수의 모양을 인쇄하는 것이 도움이 x,및 u :

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

그래서 여기에 두 가지 문제가있다. 첫 번째 것은 xt이 2 차원이어야 함에도 불구하고 1 차원임을 나타냅니다. 두 번째 것은 u이 두 번째 차원에서 t보다 하나 더 많은 요소를 가지고 있다는 것입니다. 당신은 3D 플롯을 작성하기 전에
t, x = np.meshgrid(t, x) 
u = u[:,:-1] 

을 실행하여 모두를 해결할 수 있습니다.

+0

정말 고마워요. – Javier