2017-05-17 3 views
0

Je suis nouveau à python et je suis en train de faire une surface 3D de tableau 2D, mais il semble que je suis une erreur:Essayer de tracer surf3d du tableau 2d

ValueError: shape mismatch: objects cannot be broadcast to a single shape 

Voici mon plein Code

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
import matplotlib.pyplot as plt 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 

def fourier(delX,delT): 
xMax = 10 
tMax = 12 
m = int(xMax/delX) 
n = int(tMax/delT) 
l = 0.020875 
t = 0.0 
Matrix =[[0 for x in range(m+1)]for y in range(n+1)] 
for x in range(n+1): 
Matrix[x][0] = 100 
Matrix[x][5] = 50 

for x in range(n): 
    Matrix[x+1][1] = Matrix[x][1]+l*(Matrix[x][2]-2*Matrix[x][1]+Matrix[x][0]) 
    Matrix[x+1][2] = Matrix[x][2]+l*(Matrix[x][3]-2*Matrix[x][2]+Matrix[x][1]) 
    Matrix[x+1][3] = Matrix[x][3]+l*(Matrix[x][4]-2*Matrix[x][3]+Matrix[x][2]) 
    Matrix[x+1][4] = Matrix[x][4]+l*(Matrix[x][5]-2*Matrix[x][4]+Matrix[x][3]) 

X = np.arange(0,xMax+0.1,delX) 
Y = np.arange(0,tMax+0.1,delT) 

fig = plt.figure() 
ax = fig.gca(projection='3d') 

surf = ax.plot_surface(X,Y,Matrix,cmap=cm.coolwarm,linewidth=0,antialiased=False) 
ax.set_zlim(0,101) 
ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

fig.colorbar(surf, shrink=0.5, aspect=5) 
plt.show() 

Ce qui rend la forme et comment ne correspondent pas à résoudre l'erreur?

Répondre

0

La fonction plot_surface nécessite que X, Y, Z soient des tableaux 2D, pour cela il convertit X et Y de 1D en 2D avec la fonction meshgrid.

X, Y = np.meshgrid(X, Y) 

Code complet:

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
import matplotlib.pyplot as plt 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 

def fourier(delX,delT): 
    xMax = 10 
    tMax = 12 
    m = int(xMax/delX) 
    n = int(tMax/delT) 
    l = 0.020875 
    t = 0.0 
    Matrix =[[0 for x in range(m+1)]for y in range(n+1)] 
    for x in range(n+1): 
     Matrix[x][0] = 100 
     Matrix[x][5] = 50 

    for x in range(n): 
     Matrix[x+1][1] = Matrix[x][1]+l*(Matrix[x][2]-2*Matrix[x][1]+Matrix[x][0]) 
     Matrix[x+1][2] = Matrix[x][2]+l*(Matrix[x][3]-2*Matrix[x][2]+Matrix[x][1]) 
     Matrix[x+1][3] = Matrix[x][3]+l*(Matrix[x][4]-2*Matrix[x][3]+Matrix[x][2]) 
     Matrix[x+1][4] = Matrix[x][4]+l*(Matrix[x][5]-2*Matrix[x][4]+Matrix[x][3]) 

    X = np.arange(0,xMax+0.1,delX) 
    Y = np.arange(0,tMax+0.1,delT) 

    X, Y = np.meshgrid(X, Y) 
    fig = plt.figure() 
    ax = fig.gca(projection='3d') 

    surf = ax.plot_surface(X,Y,Matrix,cmap=cm.coolwarm,linewidth=0,antialiased=False) 
    ax.set_zlim(0,101) 
    ax.zaxis.set_major_locator(LinearLocator(10)) 
    ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

    fig.colorbar(surf, shrink=0.5, aspect=5) 
    plt.show() 

fourier(0.5, 0.5) 

Sortie:

enter image description here

+0

Wow! Merci mon pote! Devrait apprendre plus de combat meshgrid maintenant –

+0

Si ma réponse aide, le marquer comme correct s'il vous plaît – eyllanesc