2016-10-05 1 views
1

Je suis en train de tracer une fonction F (x1, x2) dans matplotlib 3D, follwoing un tutoriel d'ici: http://glowingpython.blogspot.com/2012/01/how-to-plot-two-variable-functions-with.htmlEssayer de tracer la fonction multivariée dans 3D matplotlib; retour à vide figure

Une fois que je tente d'exécuter le code, la figure se révèle être vide, pas même la sortie des axes est vu. Je me demandais si quelqu'un pouvait comprendre la raison derrière ce comportement. J'utilise python 2.7

from __future__ import division 

from numpy import exp,arange 
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show 
import math 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
import matplotlib.pyplot as plt 
from matplotlib import pylab 
from numpy import arange,array,ones 
from scipy import stats 
import numpy 
import matplotlib.ticker as mtick 
import sys 
import os 


# the function that I'm going to plot 
def z_func(x1,x2): 
return exp(-(1-x1)**2 - 100*((x2-x1**2)**2)) 


x1 = arange(5.0,-5.0,-0.01) 
x2 = arange(-5.0,5.0,0.01) 
X1,X2 = meshgrid(x1, x2) # grid of point 
Z = z_func(X1, X2) # evaluation of the function on the grid 


fig = plt.figure() 
ax = fig.gca(projection='3d') 
surf = ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap=cm.RdBu,linewidth=0, antialiased=False) 

ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

ax.set_xlabel('x-axis') 
ax.set_ylabel('y-axis') 
ax.set_zlabel('z-axis') 
ax.view_init(elev=25, azim=-120) 

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

Répondre

1

Votre code fonctionne pour moi. J'avais juste besoin d'attendre que l'ordinateur termine le calcul. Le calcul long est en raison de la taille de x1 et x2. Essayez de changer ces lignes:

x1 = arange(5.0,-5.0,-0.01) 
x2 = arange(-5.0,5.0,0.01) 

aux lignes suivantes:

x1 = arange(5.0,-5.0,-0.1) 
x2 = arange(-5.0,5.0,0.1) 

P.S. Je vous conseille d'organiser vos importations. Vous avez seulement besoin de ce qui suit:

from numpy import exp, arange 
import matplotlib.pyplot as plt 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
from pylab import meshgrid 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm