Graficando una superficie

Este notebook muestra cómo graficar una superficie dada por la función: $$ f(x, y) = (4 - 2.1 x^2 + \frac 1 3 x^4) \, x^2 + x y + (4 y^2 - 4)\, y^2 $$ en $-2 < x < 2$, $-1 < y < 1$.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pylab as plt
import numpy as np

def f(x, y):
    return (4 - 2.1 * x**2 + x**4 / 3) * x**2 + x*y + (4 * y**2 - 4)*y**2

# Valores para 'x' y 'y'.
xs = np.linspace(-2, 2)
ys = np.linspace(-1, 1)

# Creamos la grilla de valores.
XX, YY = np.meshgrid(xs, ys)
ZZ = f(XX, YY)

# Graficamos la superficie.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(XX, YY, ZZ, rstride=1, cstride=1, cmap=cm.jet, linewidth=0)
# Agregamos la barra de color para la superficie.
fig.colorbar(surf, shrink=.5, aspect=5)

plt.show()